PostgreSQL 正则表达式 常用函数的总结
1
2
3
4
5
6
7
8
9
[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
正则表达式编译、匹配、释放、错误信息相关文件,后面再做具体介绍
1
2
3
4
5
6
7
8
[root@localhost regex]# pwd
/opt/hgdb-core/src/backend/regex
[root@localhost regex]# ll regIT之家.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
1
2
3
4
5
[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]#
内置函数声明:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/IT之家 src/include/catalog/pg_proc.h IT之家/
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_ ));
参数类型及返回值类型:
1
2
3
4
5
6
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 结构体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/IT之家 the biggie, a compiled RE (or rather, a front end to same) IT之家/
typedef struct
{
int re_magic; /IT之家 magic number IT之家/
size_t re_nsub; /IT之家 number of subexpressions IT之家/
long re_info; /IT之家 information about RE IT之家/
#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; /IT之家 sizeof(character) IT之家/
char IT之家re_endp; /IT之家 backward compatibility kludge IT之家/
Oid re_collation; /IT之家 Collation that defines LC_CTYPE behavior IT之家/
/IT之家 the rest is opaque pointers to hidden innards IT之家/
char IT之家re_guts; /IT之家 `char IT之家' is more portable than `void IT之家' IT之家/
char IT之家re_fns;
} regex_t;
存放编译后的正则表达式
regmatch_t 结构体
1
2
3
4
5
6
7
8
/IT之家 result reporting (may acquire more fields later) IT之家/
typedef struct
{
regoff_t rm_so; /IT之家 start of substring IT之家/
regoff_t rm_eo; /IT之家 end of substring IT之家/
} regmatch_t;
typedef long regoff_t;
成员rm_so 存放匹配文本串在目标串中的开始位置,rm_eo 存放结束位置。通常我们以数组的形式定义一组这样的结构。
有下面几个主要的函数声明
1
2
3
4
5
6
7
8
9
/IT之家
IT之家 the prototypes for exported functions
IT之家/
extern int pg_regcomp(regex_t IT之家, const pg_wchar IT之家, size_t, int, Oid);
extern int pg_regexec(regex_t IT之家, const pg_wchar IT之家, size_t, size_t, rm_detail_t IT之家, size_t, regmatch_t[], int);
extern int pg_regprefix(regex_t IT之家, pg_wchar IT之家IT之家, size_t IT之家);
extern void pg_regfree(regex_t IT之家);
extern size_t pg_regerror(int, const regex_t IT之家, char IT之家, 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 产生错误的时候,就可以调用这个函数而返回一个包含错误信息的字符串。
参数说明
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int
pg_regcomp(regex_t IT之家re,
const chr IT之家string, /IT之家 正则表达式字符串 IT之家/
size_t len, /IT之家 正则表达式字符串长度 IT之家/
int flags,
Oid collation)
int
pg_regexec(regex_t IT之家re, /IT之家 已经用regcomp函数编译好的正则表达式 IT之家/
const chr IT之家string, /IT之家 目标字符串 IT之家/
size_t len, /IT之家 目标字符串长度 IT之家/
size_t search_start, /IT之家 匹配开始位置 IT之家/
rm_detail_t IT之家details, /IT之家 NULL IT之家/
size_t nmatch, /IT之家 是regmatch_t结构体数组的长度 IT之家/
regmatch_t pmatch[], /IT之家 regmatch_t类型的结构体数组,存放匹配文本串的位置信息 IT之家/
int flags)
flags
src/backend/utils/adt/regexp.c
1
2
3
4
5
6
/IT之家 all the options of interest for regex functions IT之家/
typedef struct pg_re_flags
{
int cflags; /IT之家 compile flags for Spencer's regex code IT之家/
bool glob; /IT之家 do it globally (for each occurrence) IT之家/
} pg_re_flags;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/IT之家
IT之家 parse_re_flags - parse the options argument of regexp_matches and friends
IT之家
IT之家 flags --- output argument, filled with desired options
IT之家 opts --- TEXT object, or NULL for defaults
IT之家
IT之家 This accepts all the options allowed by any of the callers; callers that
IT之家 don't want some have to reject them after the fact.
IT之家/
static void
parse_re_flags(pg_re_flags IT之家flags, text IT之家opts)
{
/IT之家 regex flavor is always folded into the compile flags IT之家/
flags->cflags = REG_ADVANCED;
flags->glob = false;
if (opts)
{
char IT之家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': /IT之家 BREs (but why???) IT之家/
flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED | REG_QUOTE);
break;
case 'c': /IT之家 case sensitive IT之家/
flags->cflags &= ~REG_ICASE;
break;
case 'e': /IT之家 plain EREs IT之家/
flags->cflags |= REG_EXTENDED;
flags->cflags &= ~(REG_ADVANCED | REG_QUOTE);
break;
case 'i': /IT之家 case insensitive IT之家/
flags->cflags |= REG_ICASE;
break;
case 'm': /IT之家 Perloid synonym for n IT之家/
case 'n': /IT之家 affects ^ $ . [^ IT之家/
flags->cflags |= REG_NEWLINE;
break;
case 'p': /IT之家 ~Perl, affects . [^ IT之家/
flags->cflags |= REG_NLSTOP;
flags->cflags &= ~REG_NLANCH;
break;
case 'q': /IT之家 literal string IT之家/
flags->cflags |= REG_QUOTE;
flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED);
break;
case 's': /IT之家 single line, ordinary IT之家/
flags->cflags &= ~REG_NEWLINE;
break;
case 't': /IT之家 tight syntax IT之家/
flags->cflags &= ~REG_EXPANDED;
break;
case 'w': /IT之家 weird, affects ^ $ only IT之家/
flags->cflags &= ~REG_NLSTOP;
flags->cflags |= REG_NLANCH;
break;
case 'x': /IT之家 expanded syntax IT之家/
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 正则表达式 常用函数的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:https://my.oschina.net/yonj1e/blog/879875
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/sql/pgsql/8910.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
PostgreSQL中使用数组改进性
时间:2020-12-29
-
PostgreSQL安装、配置及简单
时间:2020-12-25
-
Linux CentOS 7安装PostgreSQL9
时间:2020-12-25
-
Linux CentOS 7源码编译安装
时间:2020-12-25
-
windows PostgreSQL 9.1 安装详细
时间:2020-12-25
-
什么是PostgreSQL?比MySQL、
时间:2020-12-25
-
PostgreSQL基础知识之SQL操作
时间:2020-12-25
热门文章
-
PostgreSQL基础知识之SQL操作符实践指南
时间:2020-12-25
-
windows PostgreSQL 9.1 安装详细步骤
时间:2020-12-25
-
Linux CentOS 7安装PostgreSQL9.3图文教程
时间:2020-12-25
-
PostgreSQL中使用数组改进性能实例代码
时间:2020-12-29
-
PostgreSQL安装、配置及简单使用方法
时间:2020-12-25
-
什么是PostgreSQL?比MySQL、Oracle强在哪
时间:2020-12-25
-
Linux CentOS 7源码编译安装PostgreSQL9.5
时间:2020-12-25
