C语⾔正则表达式库RegEx库
说明:
#Regular Expression Overview
. (dot) - a single character.
- the preceding character matches 0 or 1 times only.
* - the preceding character matches 0 or more times.
+ - the preceding character matches 1 or more times.
{n} - the preceding character matches exactly n times.
{n,m} - the preceding character matches at least n times and not more than m times.
[agd] - the character is one of those included within the square brackets.
[^agd] - the character is not one of those included within the square brackets.
[c-f] - the dash within the square brackets operates as a range. In this case it means either the letters c, d, e or f. () - allows us to group several characters to behave as one.
| (pipe symbol) - the logical OR operation.
^ - matches the beginning of the line.
$ - matches the end of the line. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
[a-z] Match's any single char between a to z.
[A-Z] Match's any single char between A to Z.
[0-9] Match's any single char between 0 to 9.
[a-zA-Z0-9] Match's any single character either a to z or A to Z or 0 to 9
[!@#$%^] Match's any ! or @ or # or $ or % or ^ character.
#Regular Expression Overview
C语⾔处理正则表达式常⽤的函数有regcomp()、regexec()、regfree()和regerror(),⼀般分为三个步骤,如下所⽰:
C语⾔中使⽤正则表达式⼀般分为三步:
1.编译正则表达式 regcomp()
2.匹配正则表达式 regexec()
3.释放正则表达式 regfree()
下边是对三个函数的详细解释
1、int regcomp (regex_t *compiled, const char *pattern, int cflags)
这个函数把指定的正则表达式pattern编译成⼀种特定的数据格式compiled,这样可以使匹配更有效。函数regexec 会使⽤这个数据在⽬标⽂本串中进⾏模式匹配。执⾏成功返回0。 
参数说明:
①regex_t 是⼀个结构体数据类型,⽤来存放编译后的正则表达式,它的成员re_nsub ⽤来存储正则表
达式中的⼦正则表达式的个数,
⼦正则表达式就是⽤圆括号包起来的部分表达式。
②pattern 是指向我们写好的正则表达式的指针。
③cflags 有如下4个值或者是它们或运算(|)后的值:
REG_EXTENDED 以功能更加强⼤的扩展正则表达式的⽅式进⾏匹配。
REG_ICASE 匹配字母时忽略⼤⼩写。
REG_NOSUB 不⽤存储匹配后的结果。
REG_NEWLINE 识别换⾏符,这样'$'就可以从⾏尾开始匹配,'^'就可以从⾏的开头开始匹配。
2. int regexec (regex_t *compiled, char *string, size_t nmatch, regmatch_t matchptr [], int eflags)
当我们编译好正则表达式后,就可以⽤regexec 匹配我们的⽬标⽂本串了,如果在编译正则表达式的时候没有指定cflags的参数为REG_NEWLINE,则默认情况下是忽略换⾏符的,也就是把整个⽂本串当作⼀个字符串处理。执⾏成功返回0。
regmatch_t 是⼀个结构体数据类型,在regex.h中定义:
typedef struct
{
regoff_t rm_so;
regoff_t rm_eo;
} regmatch_t;
成员rm_so 存放匹配⽂本串在⽬标串中的开始位置,rm_eo 存放结束位置。通常我们以数组的形式定义⼀组这样的结构。因为往往我们的正则表达式中还包含⼦正则表达式。数组0单元存放主正则表达式位置,后边的单元依次存放⼦正则表达式位置。
参数说明:
①compiled 是已经⽤regcomp函数编译好的正则表达式。
②string 是⽬标⽂本串。
③nmatch 是regmatch_t结构体数组的长度。
④matchptr regmatch_t类型的结构体数组,存放匹配⽂本串的位置信息。
⑤eflags 有两个值
REG_NOTBOL 按我的理解是如果指定了这个值,那么'^'就不会从我们的⽬标串开始匹配。总之我到现在还不是很明⽩这个参数的意义;
REG_NOTEOL 和上边那个作⽤差不多,不过这个指定结束end of line。
3. void regfree (regex_t *compiled)
当我们使⽤完编译好的正则表达式后,或者要重新编译其他正则表达式的时候,我们可以⽤这个函数清空compiled指向的regex_t结构体的内容,请记住,如果是重新编译的话,⼀定要先清空regex_t结构体。
4. size_t regerror (int errcode, regex_t *compiled, char *buffer, size_t length)
当执⾏regcomp 或者regexec 产⽣错误的时候,就可以调⽤这个函数⽽返回⼀个包含错误信息的字符串。
参数说明:
①errcode 是由regcomp 和 regexec 函数返回的错误代号。
②compiled 是已经⽤regcomp函数编译好的正则表达式,这个值可以为NULL。
③buffer 指向⽤来存放错误信息的字符串的内存空间。
④length 指明buffer的长度,如果这个错误信息的长度⼤于这个值,则regerror 函数会⾃动截断超出的字符串,但他仍然会返回完整的字符串的长度。所以我们可以⽤如下的⽅法先得到错误字符串的长度。
size_t length = regerror (errcode, compiled, NULL, 0);
/* return: 1, match; 0, not; -1, error */
int match(char *_string, char *_pattern)
{
int rv = 0;
regex_t *p_re = NULL;
char buf[256] = {0};
p_re = calloc(1, sizeof(regex_t));
if (p_re == NULL)
{
//printf("error: calloc: %d\n", errno);
rv = -1;
goto _end;
}
rv = regcomp(p_re, _pattern, REG_EXTENDED|REG_NOSUB);
if (rv != 0)
{
(void)regerror(rv, p_re, buf, sizeof buf);
printf("error: regcomp: %s\n",buf);
goto _end;
}
rv = regexec(p_re, _string, (size_t)0, NULL, 0);
regfree(p_re);
if (rv != 0)
{
(void)regerror(rv, p_re, buf, sizeof buf);
printf("regexec: %s, [%d],[%s]\n", buf, rv, _pattern);
goto _end;
}
rv = 1;
_end:
if (p_re != NULL)
regex匹配{
{
free(p_re);
}
return rv;
} // match
int main(int argc, char *argv[])
{
int rv = 0;
char string[128] = {0};
char pattern[64] = {0};
snprintf(string, sizeof(string), "abc123");
snprintf(pattern, sizeof(pattern), "^a.*[0-9]$");
if (argc > 2)
{
snprintf(string, sizeof(string), argv[1]);
snprintf(pattern, sizeof(pattern), argv[2]);
}
printf("pattern: [%s]\n", pattern);
rv = match(string, pattern);
if (rv == 1)
{
printf("nice, (%s) ~ (%s) matched\n", string, pattern);
}
else
{
printf("sorry, (%s) !~ (%s) not match!\n", string, pattern);    }
return rv;
} // main
// sample.c

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。