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 是一个结构体数据类型,成员rm_so 存放匹配文本串在目标串中的开始位置,rm_eo 存放结束位置。通常我们以数组的形式定义一组这样的结构。因为往往我们的规则表达式中还包含子规则表达式。数组0单元存放主规则表达式位置,后边的单元依次存放子规则表达式位置。 compiled 是已经用regcomp函数编译好的规则表达式。 string 是目标文本串。 nmatch 是regmatch_t结构体数组的长度。 matchptr regmatch_t类型的结构体数组,存放匹配文本串的位置信息。 eflags 有两个值 REG_NOTBOL 按我的理解是如果指定了这个值,那么'^'就不会从我们的目标串开始匹配。总之我到现在还不是很明白这个参数的意义, 原文如下: If this bit is set, then the beginning-of-line operator doesn't match the beginning of the string (presumably because it's not the beginning of a line).If not set, then the beginning-of-line operator does match the beginning of the string. 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);
EXAMPLES
#include <regex.h>
/*
* Match string against the extended regular expression in
* pattern, treating errors as no match.
*
* Return 1 for match, 0 for no match.
*/
int
match(const char *string, char *pattern)
{
int status;
regex_t re;
if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
return(0); /* Report error. */
}
status = regexec(&re, string, (size_t) 0, NULL, 0);
regfree(&re);
if (status != 0) {
return(0); /* Report error. */
}
return(1);
}
The following demonstrates how the REG_NOTBOL flag could be used with regexec() to find all substrings in a line that match a pattern supplied by a user. (For simplicity of the example, very little error checking is done.)
(void) regcomp (&re, pattern, 0);
/* This call to regexec() finds the first match on the line. */
error = regexec (&re, &buffer[0], 1, &pm, 0);
while (error == 0) { /* While matches found. */
/* Substring found between pm.rm_so and pm.rm_eo. */
/* This call to regexec() finds the next match. */
error = regexec (&re, buffer + pm.rm_eo, 1, &pm, REG_NOTBOL);
}
===========================================================
参考拓展:
regcomp和regexec函数
The Open Group
分享到:
相关推荐
2. int regexec (regex_t *compiled, char *string, size_t nmatch, regmatch_t match
C语言处理正则表达式常用的函数有regcomp()、regexec()、regfree()和regerror(),一般分为三个步骤,如下所示: C语言中使用正则表达式一般分为三步: 编译正则表达式 regcomp() 匹配正则表达式 regexec...
linux下posix标准的正则表达式库,支持regcomp, regexec等,可以放到vs2010或vs2012下进行编译。
正则表达式是一种用于描述字符串模式的工具,通常用于文本搜索和匹配。它可以帮助你在文本中找到特定模式的字符串,或者对文本进行替换、拆分等操作。在C语言中,正则表达式通常使用 `<regex.h>` 头文件中的函数来...
在c语言中,用regcomp、regexec、regfree 和regerror处理正则表达式。处理正则表达式分三步: 编译正则表达式,regcomp; 匹配正则表达式,regexec; 释放正则表达式,regfree。 函数原型 /* 函数说明:Regcomp...
正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来:(1)检查一个串中是否含有符合某个规则的子串,并且可以得到这个子串;(2)根据匹配规则对字符串进行灵活的替换操作。 本文介绍了正则...
一个正则表达式就是由普通字符(例如字符 a 到 z)以及特殊字符(称为元字符)组成的文字模式。该模式描述在查找文字主体时待匹配的一个或多个...正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹配。
与 PCRE 正则表达式引擎的更简单的 CPP 接口,具有全局匹配和替换。 我正在为我的 C/C++ 应用程序寻找比 regcomp 更好的正则表达式引擎。 我找到了 PCRE(Perl 兼容正则表达式, )并且对它非常满意。 它非常快。 ...
│ ├─RegComp │ │ │ Icon1.ico │ │ │ ReadMe.txt │ │ │ RegComp.clw │ │ │ RegComp.cpp │ │ │ RegComp.dsp │ │ │ RegComp.dsw │ │ │ RegComp.h │ │ │ RegComp.rc │ │ │ Resource.h │ ...