[C/C++]正则表达式

2014-11-24 11:53:50 · 作者: · 浏览: 0

首先说明,标准C/C++都不支持正则表达式,但有些函数库提供了这个功能,Philip Hazel的Perl-Compatible Regular Expression库,并且大多数Linux发行版本都带有这个函数库。

使用正则表达式可简单的分成几步:

1.编译正则表达式

2.执行匹配

3.释放内存

首先,编译正则表达式

int regcomp(regex_t *preg, const char *regex, int cflags);

reqcomp()函数用于把正则表达式编译成某种格式,可以使后面的匹配更有效。

preg: regex_t结构体用于存放编译后的正则表达式;

regex: 指向正则表达式指针;

cflags:编译模式

共有如下四种编译模式:

REG_EXTENDED:使用功能更强大的扩展正则表达式

REG_ICASE:忽略大小写

REG_NOSUB:不用存储匹配后的结果

REG_NEWLINE:识别换行符,这样‘$’就可以从行尾开始匹配,‘^’就可以从行的开头开始匹配。否则忽略换行符,把整个文本串当做一个字符串处理。

其次,执行匹配

int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags);

preg: 已编译的正则表达式指针;

string:目标字符串;

nmatch:pmatch数组的长度;

pmatch:结构体数组,存放匹配文本串的位置信息;

eflags:匹配模式

共两种匹配模式:

REG_NOTBOL:The match-beginning-of-line operator always fails to match (but see the compilation flag REG_NEWLINE above). This flag may be used when different portions of a string are passed to regexec and the beginning of the string should not be interpreted as the beginning of the line.

REG_NOTEOL:The match-end-of-line operator always fails to match (but see the compilation flag REG_NEWLINE above)

最后,释放内存
void regfree(regex_t *preg);
当使用完编译好的正则表达式后,或者需要重新编译其他正则表达式时,一定要使用这个函数清空该变量。


其他,处理错误
size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);
当执行regcomp 或者regexec 产生错误的时候,就可以调用这个函数而返回一个包含错误信息的字符串。
errcode: 由regcomp 和 regexec 函数返回的错误代号。
preg: 已经用regcomp函数编译好的正则表达式,这个值可以为NULL。
errbuf: 指向用来存放错误信息的字符串的内存空间。
errbuf_size: 指明buffer的长度,如果这个错误信息的长度大于这个值,则regerror 函数会自动截断超出的字符串,但他仍然会返回完整的字符串的长度。所以我们可以用如下的方法先得到错误字符串的长度。

示例

01
#include
02
#include
03
#include
04
#include
05

06
int validate_pattern__r(const char *pattern, const char *in_str);
07

08
int main(int argc, char **argv)
09
{
10
int result = -1;
11
char patt[] = "^([2-9][0-9]{3}|[1-9][0-9]{4,6}|10000000)$";
12
char str[] = "10000000";
13

14
result = validate_pattern__r(patt, str);
15
if(0 == result)
16
{
17
printf("OK, match!\n");
18
}
19
else
20
{
21
printf("NOK, not match!\n");
22
}
23

24
return 0;
25
}
26

27
int validate_pattern__r(const char *pattern, const char *in_str)
28
{
29
int l_status = 0;
30
regex_t l_re;
31

32
printf("pattern: %s\n", pattern);
33
printf("in_str: %s\n", in_str);
34

35
regcomp(&l_re, pattern, REG_EXTENDED);
36
l_status = regexec(&l_re, in_str, (size_t)0, NULL, 0);
37
regfree(&l_re);
38
if(0 != l_status)
39
{
40
return -1;
41
}
42

43
return 0;
44
}

作者:恋恋美食