PostgreSQL 正则表达式 常用函数的总结

对那些需要进行复杂数据处理的程序来说,正则表达式无疑是一个非常有用的工具。本文重点在于阐述 PostgreSQL 的一些常用正则表达式函数以及源码中的一些函数。
正则相关部分的目录结构
[root@localhost regex]# pwd /opt/hgdb-core/src/include/regex [root@localhost regex]# ll total 40 -rw-r--r--. 1 postgres postgres 3490 Mar 19 19:00 regcustom.h -rw-r--r--. 1 postgres postgres 1332 Mar 19 18:59 regerrs.h -rw-r--r--. 1 postgres postgres 6703 Mar 19 19:00 regex.h -rw-r--r--. 1 postgres postgres 2353 Mar 19 19:00 regexport.h -rw-r--r--. 1 postgres postgres 16454 Mar 19 19:00 regguts.h
正则表达式编译、匹配、释放、错误信息相关文件,后面再做具体介绍
[root@localhost regex]# pwd /opt/hgdb-core/src/backend/regex [root@localhost regex]# ll reg*.c -rw-r--r--. 1 postgres postgres 55851 Mar 19 19:00 regcomp.c -rw-r--r--. 1 postgres postgres 3671 Mar 19 18:59 regerror.c -rw-r--r--. 1 postgres postgres 34873 Mar 19 19:00 regexec.c -rw-r--r--. 1 postgres postgres 2123 Mar 19 18:59 regfree.c [root@localhost regex]#
内置函数实现在 regexp.c
[root@localhost adt]# pwd /opt/hgdb-core/src/backend/utils/adt [root@localhost adt]# ll regexp.c -rw-r--r--. 1 postgres postgres 34863 Apr 12 02:29 regexp.c [root@localhost adt]#
内置函数声明:
/* src/include/catalog/pg_proc.h */
DATA(insert OID = 2073 ( substring PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 25 "25 25" _null_ _null_ _null_ _null_ _null_ textregexsubstr _null_ _null_ _null_ ));
DESCR("extract text matching regular expression");
DATA(insert OID = 2074 ( substring PGNSP PGUID 14 1 0 0 0 f f f f t f i 3 0 25 "25 25 25" _null_ _null_ _null_ _null_ _null_ "select pg_catalog.substring($1, pg_catalog.similar_escape($2, $3))" _null_ _null_ _null_ ));
DESCR("extract text matching SQL99 regular expression");
DATA(insert OID = 2284 ( regexp_replace PGNSP PGUID 12 1 0 0 0 f f f f t f i 3 0 25 "25 25 25" _null_ _null_ _null_ _null_ _null_ textregexreplace_noopt _null_ _null_ _null_ ));
DESCR("replace text using regexp");
DATA(insert OID = 2285 ( regexp_replace PGNSP PGUID 12 1 0 0 0 f f f f t f i 4 0 25 "25 25 25 25" _null_ _null_ _null_ _null_ _null_ textregexreplace _null_ _null_ _null_ ));
DESCR("replace text using regexp");
DATA(insert OID = 2763 ( regexp_matches PGNSP PGUID 12 1 1 0 0 f f f f t t i 2 0 1009 "25 25" _null_ _null_ _null_ _null_ _null_ regexp_matches_no_flags _null_ _null_ _null_ ));
DESCR("find all match groups for regexp");
DATA(insert OID = 2764 ( regexp_matches PGNSP PGUID 12 1 10 0 0 f f f f t t i 3 0 1009 "25 25 25" _null_ _null_ _null_ _null_ _null_ regexp_matches _null_ _null_ _null_ ));
DESCR("find all match groups for regexp");
DATA(insert OID = 2765 ( regexp_split_to_table PGNSP PGUID 12 1 1000 0 0 f f f f t t i 2 0 25 "25 25" _null_ _null_ _null_ _null_ _null_ regexp_split_to_table_no_flags _null_ _null_ _null_ ));
DESCR("split string by pattern");
DATA(insert OID = 2766 ( regexp_split_to_table PGNSP PGUID 12 1 1000 0 0 f f f f t t i 3 0 25 "25 25 25" _null_ _null_ _null_ _null_ _null_ regexp_split_to_table _null_ _null_ _null_ ));
DESCR("split string by pattern");
DATA(insert OID = 2767 ( regexp_split_to_array PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 1009 "25 25" _null_ _null_ _null_ _null_ _null_ regexp_split_to_array_no_flags _null_ _null_ _null_ ));
DESCR("split string by pattern");
DATA(insert OID = 2768 ( regexp_split_to_array PGNSP PGUID 12 1 0 0 0 f f f f t f i 3 0 1009 "25 25 25" _null_ _null_ _null_ _null_ _null_ regexp_split_to_array _null_ _null_ _null_ ));
参数类型及返回值类型:
postgres=# select oid,typname from pg_type where oid = 25 or oid = 1009; oid | typname ------+--------- 25 | text 1009 | _text (2 rows)
substring(string from pattern)函数提供了从字符串中抽取一个匹配 POSIX 正则表达式模式的子字符串的方法。如果没有匹配它返回 NULL ,否则就是文本中匹配模式的那部分。
regexp_replace(source, pattern, replacement [, flags ])函数提供了将匹配 POSIX 正则表达式模式的子字符串替换为新文本的功能。
regexp_matches(string, pattern[, flags ])函数返回一个从匹配POSIX正则表达式模式中获取的所有子串结果的text数组。
参数flags是一个可选的text字符串,含有0或者更多单字母标记来改变函数行为。标记g导致查找字符串中的每个匹配,而不仅是第一个,每个匹配返回一行。
regexp_split_to_table(string, pattern[, flags ])函数使用POSIX正则表达式模式作为分隔符,分隔字符串。返回结果为string。。
regexp_split_to_array (string, pattern[, flags ])函数与regexp_split_to_table行为相同,但,返回结果为text数组。
具体使用参考用户手册。
src/include/regex/regex.h
regex_t 结构体
/* the biggie, a compiled RE (or rather, a front end to same) */
typedef struct
{
int re_magic; /* magic number */
size_t re_nsub; /* number of subexpressions */
long re_info; /* information about RE */
#define REG_UBACKREF 000001
#define REG_ULOOKAHEAD 000002
#define REG_UBOUNDS 000004
#define REG_UBRACES 000010
#define REG_UBSALNUM 000020
#define REG_UPBOTCH 000040
#define REG_UBBS 000100
#define REG_UNONPOSIX 000200
#define REG_UUNSPEC 000400
#define REG_UUNPORT 001000
#define REG_ULOCALE 002000
#define REG_UEMPTYMATCH 004000
#define REG_UIMPOSSIBLE 010000
#define REG_USHORTEST 020000
int re_csize; /* sizeof(character) */
char *re_endp; /* backward compatibility kludge */
Oid re_collation; /* Collation that defines LC_CTYPE behavior */
/* the rest is opaque pointers to hidden innards */
char *re_guts; /* `char *' is more portable than `void *' */
char *re_fns;
} regex_t;
存放编译后的正则表达式
regmatch_t 结构体
/* result reporting (may acquire more fields later) */
typedef struct
{
regoff_t rm_so; /* start of substring */
regoff_t rm_eo; /* end of substring */
} regmatch_t;
typedef long regoff_t;
成员rm_so 存放匹配文本串在目标串中的开始位置,rm_eo 存放结束位置。通常我们以数组的形式定义一组这样的结构。
有下面几个主要的函数声明
/* * the prototypes for exported functions */ extern int pg_regcomp(regex_t *, const pg_wchar *, size_t, int, Oid); extern int pg_regexec(regex_t *, const pg_wchar *, size_t, size_t, rm_detail_t *, size_t, regmatch_t[], int); extern int pg_regprefix(regex_t *, pg_wchar **, size_t *); extern void pg_regfree(regex_t *); extern size_t pg_regerror(int, const regex_t *, char *, size_t); extern void pg_set_regex_collation(Oid collation);
处理正则表达式常用的函数有 pg_regcomp()、pg_regexec()、pg_regfree() 和 pg_regerror()。
一般处理步骤:编译正则表达式 pg_regcomp(),匹配正则表达式 pg_regexec(),释放正则表达式 pg_regfree()。
pg_regerror() :当执行regcomp 或者regexec 产生错误的时候,就可以调用这个函数而返回一个包含错误信息的字符串。
参数说明
int pg_regcomp(regex_t *re, const chr *string, /* 正则表达式字符串 */ size_t len, /* 正则表达式字符串长度 */ int flags, Oid collation) int pg_regexec(regex_t *re, /* 已经用regcomp函数编译好的正则表达式 */ const chr *string, /* 目标字符串 */ size_t len, /* 目标字符串长度 */ size_t search_start, /* 匹配开始位置 */ rm_detail_t *details, /* NULL */ size_t nmatch, /* 是regmatch_t结构体数组的长度 */ regmatch_t pmatch[], /* regmatch_t类型的结构体数组,存放匹配文本串的位置信息 */ int flags)
flags
src/backend/utils/adt/regexp.c
/* all the options of interest for regex functions */
typedef struct pg_re_flags
{
int cflags; /* compile flags for Spencer's regex code */
bool glob; /* do it globally (for each occurrence) */
} pg_re_flags;
/*
* parse_re_flags - parse the options argument of regexp_matches and friends
*
* flags --- output argument, filled with desired options
* opts --- TEXT object, or NULL for defaults
*
* This accepts all the options allowed by any of the callers; callers that
* don't want some have to reject them after the fact.
*/
static void
parse_re_flags(pg_re_flags *flags, text *opts)
{
/* regex flavor is always folded into the compile flags */
flags->cflags = REG_ADVANCED;
flags->glob = false;
if (opts)
{
char *opt_p = VARDATA_ANY(opts);
int opt_len = VARSIZE_ANY_EXHDR(opts);
int i;
for (i = 0; i < opt_len; i++)
{
switch (opt_p[i])
{
case 'g':
flags->glob = true;
break;
case 'b': /* BREs (but why???) */
flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED | REG_QUOTE);
break;
case 'c': /* case sensitive */
flags->cflags &= ~REG_ICASE;
break;
case 'e': /* plain EREs */
flags->cflags |= REG_EXTENDED;
flags->cflags &= ~(REG_ADVANCED | REG_QUOTE);
break;
case 'i': /* case insensitive */
flags->cflags |= REG_ICASE;
break;
case 'm': /* Perloid synonym for n */
case 'n': /* \n affects ^ $ . [^ */
flags->cflags |= REG_NEWLINE;
break;
case 'p': /* ~Perl, \n affects . [^ */
flags->cflags |= REG_NLSTOP;
flags->cflags &= ~REG_NLANCH;
break;
case 'q': /* literal string */
flags->cflags |= REG_QUOTE;
flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED);
break;
case 's': /* single line, \n ordinary */
flags->cflags &= ~REG_NEWLINE;
break;
case 't': /* tight syntax */
flags->cflags &= ~REG_EXPANDED;
break;
case 'w': /* weird, \n affects ^ $ only */
flags->cflags &= ~REG_NLSTOP;
flags->cflags |= REG_NLANCH;
break;
case 'x': /* expanded syntax */
flags->cflags |= REG_EXPANDED;
break;
default:
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid regexp option: \"%c\"",
opt_p[i])));
break;
}
}
}
}
| 选项 | 描述 |
| b | 剩余的正则表达式是 BR |
| c | 大小写敏感匹配(覆盖操作符类型) |
| e | 剩余的正则表达式是 ERE |
| i | 大小写不敏感匹配(覆盖操作符类型) |
| m | n的历史同义词 |
| n | 新行敏感匹 |
| p | 部分新行敏感匹配 |
| q | 重置正则表达式为一个文本("引起")字符串,所有都是普通字符。 |
| s | 非新行敏感匹配(缺省) |
| t | 紧语法 |
| w | 反转部分新行敏感("怪异")匹配 |
| x | 扩展的语法 |
以上就是PostgreSQL 正则表达式 常用函数的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
# PostgreSQL
# 正则表达式
# 正则表达式常用函数
# PostgreSQL 创建表分区
# PostgreSQL 查看数据库
# 索引
# 表
# 表空间大小的示例代码
# PostgreSQL 查找当前数据库的所有表操作
# postgresql 实现字符串分割字段转列表查询
# postgresql 实现获取所有表名
# 字段名
# 字段类型
# 注释
# PostgreSQL之分区表(partitioning)
# 浅谈PostgreSQL表分区的三种方式
# PostgreSQL 临时表空间的实现
# 错误信息
# 都是
# 是一个
# 几个
# 第一个
# 如有
# 而不
# 希望能
# 如果没有
# 数据处理
# 仅是
# 可选
# 谢谢大家
# 再做
# 是一个非常
# 就可以
# 返回值
# 用户手册
# 疑问请
相关文章:
,有什么在线背英语单词效率比较高的网站?
大连企业网站制作公司,大连2025企业社保缴费网上缴费流程?
湖南网站制作公司,湖南上善若水科技有限公司做什么的?
沈阳个人网站制作公司,哪个网站能考到沈阳事业编招聘的信息?
Swift中swift中的switch 语句
如何快速打造个性化非模板自助建站?
完全自定义免费建站平台:主题模板在线生成一站式服务
开封网站制作公司,网络用语开封是什么意思?
如何用IIS7快速搭建并优化网站站点?
如何规划企业建站流程的关键步骤?
网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?
昆明网站制作哪家好,昆明公租房申请网上登录入口?
建站之星代理如何优化在线客服效率?
相册网站制作软件,图片上的网址怎么复制?
开心动漫网站制作软件下载,十分开心动画为何停播?
如何快速搭建高效WAP手机网站?
如何通过FTP服务器快速搭建网站?
广州建站公司哪家好?十大优质服务商推荐
网站代码制作软件有哪些,如何生成自己网站的代码?
如何在宝塔面板中创建新站点?
建站主机如何选?高性价比方案全解析
如何通过宝塔面板实现本地网站访问?
昆明高端网站制作公司,昆明公租房申请网上登录入口?
寿县云建站:智能SEO优化与多行业模板快速上线指南
专业网站建设制作报价,网页设计制作要考什么证?
Thinkphp 中 distinct 的用法解析
长沙做网站要多少钱,长沙国安网络怎么样?
深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?
山东云建站价格为何差异显著?
高防服务器如何保障网站安全无虞?
如何在Windows 2008云服务器安全搭建网站?
微网站制作教程,我微信里的网站怎么才能复制到浏览器里?
网站制作价目表怎么做,珍爱网婚介费用多少?
如何获取免费开源的自助建站系统源码?
实现点击下箭头变上箭头来回切换的两种方法【推荐】
如何在IIS中新建站点并解决端口绑定冲突?
网站制作员失业,怎样查看自己网站的注册者?
郑州企业网站制作公司,郑州招聘网站有哪些?
武汉网站设计制作公司,武汉有哪些比较大的同城网站或论坛,就是里面都是武汉人的?
建站之星如何助力网站排名飙升?揭秘高效技巧
建站之星导航菜单设置与功能模块配置全攻略
建站之星多图banner生成与模板自定义指南
Swift开发中switch语句值绑定模式
制作表格网站有哪些,线上表格怎么弄?
建站之星代理平台如何选择最佳方案?
宝塔新建站点为何无法访问?如何排查?
建站主机选购指南:核心配置与性价比推荐解析
如何配置IIS站点权限与局域网访问?
广州顶尖建站服务:企业官网建设与SEO优化一体化方案
行程制作网站有哪些,第三方机票电子行程单怎么开?
*请认真填写需求信息,我们会在24小时内与您取得联系。