refer: http://old.blog.edu.cn/user2/linxh/archives/2006/1438032.shtml
原文转自: http://www.devfront.com:8080/?q=node/140
GNU/Linux有两套库可用于正则表达式编程:POSIX库和PCRE库。前者不需要单独安装,一般需求还是能满足的,速度稍慢些。后者是久负盛名的Perl正则表达式库,功能强大,匹配速度快,不过可能需要单独安装。
我们先用一个例子来介绍如何使用POSIX库。
#i nclude <stdio.h>
#i nclude <sys/types.h>
#i nclude <regex.h>
char *get_regerror (int errcode, regex_t *compiled)
{
size_t length = regerror (errcode, compiled, NULL, 0);
char *buffer = malloc(length);
if (!buffer) return NULL;
(void) regerror (errcode, compiled, buffer, length);
return buffer;
}
int regtest(const char*pattern, const char* string)
{
regex_t reg;
regmatch_t *subexprs = NULL;
int ret;
int i;
if (0 != (ret=regcomp(?, pattern, REG_EXTENDED))) {
char *buffer = get_regerror(ret, ?);
if (buffer) {
fprintf(stderr, "regcomp:[%d]%s\n", ret, buffer);
free(buffer);
}
return -1;
}
subexprs = malloc((reg.re_nsub+1)*sizeof(regmatch_t));
if (!subexprs) {
fprintf(stderr, "error malloc subexprs\n");
regfree(?);
return -1;
}
if (0 != (ret=regexec(?, string, reg.re_nsub+1, subexprs, 0))) {
char *buffer = get_regerror(ret, ?);
if (buffer) {
fprintf(stderr, "regexec:[%d]%s\n", ret, buffer);
free(buffer);
}
regfree(?);
return -1;
}
for (i = 0; i <= reg.re_nsub; i++) {
printf("[%d]:", i);
if (subexprs[i].rm_so == subexprs[i].rm_eo) {
printf("[EMPTY SUBEXPR]\n");
}
else if (subexprs[i].rm_so == -1 ||
subexprs[i].rm_eo == -1) {
printf("[NO SUBEXPR]\n");
}
else {
fwrite(string+subexprs[i].rm_so, 1,
subexprs[i].rm_eo-subexprs[i].rm_so, stdout);
printf("\n");
}
}
regfree(?);
if (subexprs) free(subexprs);
return 0;
}
int main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: regtest pattern string\n");
return -1;
}
fprintf(stderr, "pattern:%s\n", argv[1]);
fprintf(stderr, "string:%s\n", argv[2]);
return regtest(argv[1], argv[2]);
}
在字符串匹配之前,我们必须先编译匹配模式,这是通过regcomp实现的。这个函数的原型如下:
int regcomp (regex_t *compiled, const char *pattern, int cflags)
参数compiled只有一个成员是我们需要关注的,那就是re_nsub,代表编译后的子表达式数目,由于我们还需要保存整个匹配到的模式,所以最终匹配的条目数是re_nsub加1。cflags用来修饰匹配模式,可取值如下:
REG_EXTENDED 启用POSIX正则库扩展,关于该扩展的详细信息可参考POSIX规范
REG_ICASE 忽略大小写
REG_NOSUB 不要存储子表达式
REG_NEWLINE 把换行符作为多行的分隔符,这样'$'可匹配每一行的行尾,'^'匹配每一行的行首,'.'不匹配换行符,[^...]不匹配新行
编译完模式后我们从内存中分配子表达式存储空间,然后调用regexec对串进行匹配,该函数原型如下:
int regexec (regex_t *compiled, char *string, size_t nmatch, regmatch_t matchptr [], int eflags)
nmatch指明matchptr数组的数目,该数目是compiled->re_nsub+1,也可以让nmatch为0,matchptr为NULL,表示不要保存子表达式。eflags通常为0。
匹配结束后,匹配到的子表达式在串中的偏移保存在regmatch_t结构中,该结构有两个成员:
rm_so 子表达式的起始偏移
rm_eo 子表达式的结束偏移
这是一个开区间,实际的子表达式在[rm_so,rm_eo)里。
如果没有匹配的子表达式,比如"f(o*)"匹配"fum",实际匹配到的只有"f",这时rm_so和rm_eo相等,都为1。如果整个模式在没有子表示式的情况下也能匹配,这时rm_so和rm_eo为-1,比如"ba(na)*"匹配"ba"。
POSIX正则库表述到这里,下面我们看看PCRE库。
PCRE库的功能虽然强大,可是并不难使用。详细信息可参考http://www.pcre.org/里的文档,附录有一个例子,比较全面地阐述了如何调用该库。
PCRE的子表达式和POSIX类似,不过它还引入了一个命名子表达式的概念。比如模式"(?P(?P(\d\d)?\d\d)-(?P\d\d)-(?P\d\d))",其中的date, year, month, day是对子表达式的命名。如何根据这些名称来获取子表达式,PCRE文档有详细的说明。
附录:pcredemo.c
/*************************************************
* PCRE DEMONSTRATION PROGRAM *
*************************************************/
/* This is a demonstration program to illustrate the most straightforward ways
of calling the PCRE regular expression library from a C program. See the
pcresample documentation for a short discussion.
Compile thuswise:
gcc -Wall pcredemo.c -I/usr/local/include -L/usr/local/lib \
-R/usr/local/lib -lpcre
Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and
library files for PCRE are installed on your system. Only some operating
systems (e.g. Solaris) use the -R option.
*/
#i nclude <stdio.h>
#i nclude <string.h>
#i nclude <pcre.h>
#define OVECCOUNT 30 /* should be a multiple of 3 */
int main(int argc, char **argv)
{
pcre *re;
const char *error;
char *pattern;
char *subject;
unsigned char *name_table;
int erroffset;
int find_all;
int namecount;
int name_entry_size;
int ovector[OVECCOUNT];
int subject_length;
int rc, i;
/**************************************************************************
* First, sort out the command line. There is only one possible option at *
* the moment, "-g" to request repeated matching to find all occurrences, *
* like Perl's /g option. We set the variable find_all to a non-zero value *
* if the -g option is present. Apart from that, there must be exactly two *
* arguments. *
**************************************************************************/
find_all = 0;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-g") == 0) find_all = 1;
else break;
}
/* After the options, we require exactly two arguments, which are the pattern,
and the subject string. */
if (argc - i != 2) {
printf("Two arguments required: a regex and a subject string\n");
return 1;
}
pattern = argv[i];
subject = argv[i+1];
subject_length = (int)strlen(subject);
/*************************************************************************
* Now we are going to compile the regular expression pattern, and handle *
* and errors that are detected. *
*************************************************************************/
re = pcre_compile(
pattern, /* the pattern */
0, /* default options */
&error, /* for error message */
&erroffset, /* for error offset */
NULL); /* use default character tables */
/* Compilation failed: print the error message and exit */
if (re == NULL) {
printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
return 1;
}
/*************************************************************************
* If the compilation succeeded, we call PCRE again, in order to do a *
* pattern match against the subject string. This does just ONE match. If *
* further matching is needed, it will be done below. *
*************************************************************************/
rc = pcre_exec(
re, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
subject, /* the subject string */
subject_length, /* the length of the subject */
0, /* start at offset 0 in the subject */
0, /* default options */
ovector, /* output vector for substring information */
OVECCOUNT); /* number of elements in the output vector */
/* Matching failed: handle error cases */
if (rc < 0) {
switch(rc){
case PCRE_ERROR_NOMATCH: printf("No match\n"); break;
/*
Handle other special cases if you like
*/
default: printf("Matching error %d\n", rc); break;
}
free(re); /* Release memory used for the compiled pattern */
return 1;
}
/* Match succeded */
printf("\nMatch succeeded at offset %d\n", ovector[0]);
/*************************************************************************
* We have found the first match within the subject string. If the output *
* vector wasn't big enough, set its size to the maximum. Then output any *
* substrings that were captured. *
*************************************************************************/
/* The output vector wasn't big enough */
if (rc == 0) {
rc = OVECCOUNT/3;
printf("ovector only has room for %d captured substrings\n", rc - 1);
}
/* Show substrings stored in the output vector by number. Obviously, in a real
application you might want to do things other than print them. */
for (i = 0; i < rc; i++){
char *substring_start = subject + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
printf("%2d: %.*s\n", i, substring_length, substring_start);
}
/**************************************************************************
* That concludes the basic part of this demonstration program. We have *
* compiled a pattern, and performed a single match. The code that follows *
* first shows how to access named substrings, and then how to code for *
* repeated matches on the same subject. *
**************************************************************************/
/* See if there are any named substrings, and if so, show them by name. First
we have to extract the count of named parentheses from the pattern. */
(void)pcre_fullinfo(
re, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
PCRE_INFO_NAMECOUNT, /* number of named substrings */
&namecount); /* where to put the answer */
if (namecount <= 0) printf("No named substrings\n"); else
{
unsigned char *tabptr;
printf("Named substrings\n");
/* Before we can access the substrings, we must extract the table for
translating names to numbers, and the size of each entry in the table. */
(void)pcre_fullinfo(
re, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
PCRE_INFO_NAMETABLE, /* address of the table */
&name_table); /* where to put the answer */
(void)pcre_fullinfo(
re, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
PCRE_INFO_NAMEENTRYSIZE, /* size of each entry in the table */
&name_entry_size); /* where to put the answer */
/* Now we can scan the table and, for each entry, print the number, the name,
and the substring itself. */
tabptr = name_table;
for (i = 0; i < namecount; i++){
int n = (tabptr[0] << 8) | tabptr[1];
printf("(%d) %*s: %.*s\n", n, name_entry_size - 3, tabptr + 2,
ovector[2*n+1] - ovector[2*n], subject + ovector[2*n]);
tabptr += name_entry_size;
}
}
/*************************************************************************
* If the "-g" option was given on the command line, we want to continue *
* to search for additional matches in the subject string, in a similar *
* way to the /g option in Perl. This turns out to be trickier than you *
* might think because of the possibility of matching an empty string. *
* What happens is as follows: *
* *
* If the previous match was NOT for an empty string, we can just start *
* the next match at the end of the previous one. *
* *
* If the previous match WAS for an empty string, we can't do that, as it *
* would lead to an infinite loop. Instead, a special call of pcre_exec() *
* is made with the PCRE_NOTEMPTY and PCRE_ANCHORED flags set. The first *
* of these tells PCRE that an empty string is not a valid match; other *
* possibilities must be tried. The second flag restricts PCRE to one *
* match attempt at the initial string position. If this match succeeds, *
* an alternative to the empty string match has been found, and we can *
* proceed round the loop. *
*************************************************************************/
if (!find_all){
free(re); /* Release the memory used for the compiled pattern */
return 0; /* Finish unless -g was given */
}
/* Loop for second and subsequent matches */
for (;;){
int options = 0; /* Normally no options */
int start_offset = ovector[1]; /* Start at end of previous match */
/* If the previous match was for an empty string, we are finished if we are
at the end of the subject. Otherwise, arrange to run another match at the
same point to see if a non-empty match can be found. */
if (ovector[0] == ovector[1]){
if (ovector[0] == subject_length) break;
options = PCRE_NOTEMPTY | PCRE_ANCHORED;
}
/* Run the next matching operation */
rc = pcre_exec(
re, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
subject, /* the subject string */
subject_length, /* the length of the subject */
start_offset, /* starting offset in the subject */
options, /* options */
ovector, /* output vector for substring information */
OVECCOUNT); /* number of elements in the output vector */
/* This time, a result of NOMATCH isn't an error. If the value in "options"
is zero, it just means we have found all possible matches, so the loop ends.
Otherwise, it means we have failed to find a non-empty-string match at a
point where there was a previous empty-string match. In this case, we do what
Perl does: advance the matching position by one, and continue. We do this by
setting the "end of previous match" offset, because that is picked up at the
top of the loop as the point at which to start again. */
if (rc == PCRE_ERROR_NOMATCH){
if (options == 0) break;
ovector[1] = start_offset + 1;
continue; /* Go round the loop again */
}
/* Other matching errors are not recoverable. */
if (rc < 0){
printf("Matching error %d\n", rc);
free(re); /* Release memory used for the compiled pattern */
return 1;
}
/* Match succeded */
printf("\nMatch succeeded again at offset %d\n", ovector[0]);
/* The match succeeded, but the output vector wasn't big enough. */
if (rc == 0){
rc = OVECCOUNT/3;
printf("ovector only has room for %d captured substrings\n", rc - 1);
}
/* As before, show substrings stored in the output vector by number, and then
also any named substrings. */
for (i = 0; i < rc; i++){
char *substring_start = subject + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
printf("%2d: %.*s\n", i, substring_length, substring_start);
}
if (namecount <= 0) printf("No named substrings\n"); else
{
unsigned char *tabptr = name_table;
printf("Named substrings\n");
for (i = 0; i < namecount; i++)
{
int n = (tabptr[0] << 8) | tabptr[1];
printf("(%d) %*s: %.*s\n", n, name_entry_size - 3, tabptr + 2,
ovector[2*n+1] - ovector[2*n], subject + ovector[2*n]);
tabptr += name_entry_size;
}
}
} /* End of loop to find second and subsequent matches */
printf("\n");
free(re); /* Release memory used for the compiled pattern */
return 0;
}
/* End of pcredemo.c */
参考资料:
1.The GNU C Library
2. http://www.pcre.org/pcre.txt
分享到:
相关推荐
总的来说,"C/C++正则表达式boost_1_34_1源码"是一个宝贵的学习资源,它不仅提供了强大的文本处理工具,还展示了高级C++编程技巧和设计模式。对于想要提升C++编程技能,尤其是对正则表达式感兴趣的开发者来说,这是...
《最小的C++正则表达式库:高效与便携性的完美融合》 在软件开发领域,正则表达式作为一种强大的文本处理工具,被广泛应用于数据验证、文本搜索和替换等场景。对于C++程序员来说,找到一个轻量级、跨平台且功能强大...
在C/C++编程环境中,正则表达式(Regex或Regular Expression)是一种强大的文本处理工具,用于模式匹配、字符串查找、替换等操作。由于C++标准库并没有内置完整的正则表达式支持,直到C++11标准引入了`<regex>`库,...
在C和C++编程语言中,正则表达式(Regular Expression,简称regex)是一种强大的文本处理工具,用于模式匹配和字符串搜索。`regex.h`和`regex.c`这两个文件是实现正则表达式功能的核心部分,通常在C语言中,由于标准...
在C++编程中,正则表达式是一种强大的文本处理工具,它允许我们通过模式匹配来查找、替换或分割字符串。在Visual Studio 2008(VS2008)这个IDE环境下,C++并不直接支持标准库中的正则表达式,但可以通过引入外部库...
在C++编程中,正则表达式是一种强大的文本处理工具,它允许程序员通过模式匹配来查找、替换或分割字符串。正则表达式引擎通常被封装在标准库中,C++11及其后续版本引入了`<regex>`库,使得开发者可以直接在C++中使用...
C++中的正则表达式是通过标准库 `<regex>` 提供的功能,允许程序员进行复杂的文本匹配和处理。正则表达式是一种模式匹配工具,能够快速有效地查找、替换或分割字符串。在C++中,理解并熟练运用正则表达式可以极大地...
正则表达式(Regular Expression,简称regex)是编程领域中一种强大的文本处理工具,它用于在字符串中查找、...通过学习和使用这些文件,你可以掌握如何利用正则表达式进行复杂的文本处理任务,提升你的C语言编程能力。
C++正则表达式是C++编程语言中用于处理文本模式匹配的重要工具,它允许开发者在字符串中查找、替换或提取符合特定规则的模式。在C++中,正则表达式的实现通常依赖于第三方库,如PCRE(Perl Compatible Regular ...
在MFC中,你可以使用`ATL::CRegEx`类来处理正则表达式。这个类提供了构造、编译、执行和解析正则表达式的方法。 例如,在`mfcregex3.cpp`和`mfcregex3.h`文件中,我们可能会看到如下代码: ```cpp #include ...
在C++编程语言中,实现正则表达式解析类可以提供一套高效且灵活的接口,使得开发者能够轻松地处理复杂的字符串操作。这个压缩包中的"正则表达式解析类"应该是一个实现了正则表达式功能的自定义类库,方便用户在C++...
例如,"cat" 这个正则表达式可以匹配任何包含连续的 "c"、"a" 和 "t" 的字符串。正则表达式不仅限于单个字符,还可以包含元字符和特殊构造,使得匹配更为灵活。 2. **正则表达式引擎** 正则表达式引擎是处理正则...
在编程语言和文本编辑器中,正则表达式经常用于数据提取、输入验证等场景。以下是一些基本的正则表达式概念和符号: 1. 字符匹配:"."代表任意单个字符,"*"表示前面的字符可以出现零次或多次。 2. 重复:"{n}"表示...
在C语言中,虽然标准库并不直接支持正则表达式,但通过第三方库,如Perl-Compatible Regular Expression(PCRE)库,程序员可以实现正则表达式功能。PCRE库由Philip Hazel开发,广泛应用于各种Linux发行版,为C/C++...
在C++编程中,正则表达式是一种强大的文本处理工具,可以用来匹配、查找、替换等操作复杂的字符串模式。Boost库是C++的一个开源库集合,它为C++程序员提供了许多高级功能,其中包括一个功能丰富的正则表达式库。本...
在Linux操作系统中,C语言编程时常常需要处理字符串匹配和模式查找的问题,这时POSIX(Portable Operating System Interface)正则表达式库就显得尤为重要。POSIX正则表达式是符合IEEE Std 1003.1标准的一套规则,它...
在C++编程环境中,虽然标准库并没有内置正则表达式支持,但通过Microsoft Visual C++(VC++)环境,我们可以利用其提供的库来实现正则表达式的功能。"VC++正则表达式测试工具"正是这样一个实用的小程序,它可以帮助...