原帖由 hoxide 于 2004-4-17 18:54 发表
我最近看SCO OpenServer程序员参考才知道有c中的正则表达式处理库的,没想到这里楼主也介绍了.
好,支持加精
在调用函数regexec()进行模式匹配的过程中,可能在字符串string中会有多处与给定的正则表达式相匹配,参数pmatch就是用来保存这些匹配位置的,而参数nmatch则告诉函数regexec()最多可以把多少个匹配结果填充到pmatch数组中。当regexec()函数成功返回时,从 string+pmatch[0].rm_so到string+pmatch[0].rm_eo是第一个匹配的字符串,而从string+pmatch [1].rm_so到string+pmatch[1].rm_eo,则是第二个匹配的字符串,依此类推。
10.3.4 Match Results with Subexpressions
When regexec matches parenthetical subexpressions of pattern, it records which parts of string they match. It returns that information by storing the offsets into an array whose elements are structures of type regmatch_t. The first element of the array (index 0) records the part of the string that matched the entire regular expression. Each other element of the array records the beginning and end of the part that matched a single parenthetical subexpression.
— Data Type: regmatch_t
This is the data type of the matcharray array that you pass to regexec. It contains two structure fields, as follows:
rm_so
The offset in string of the beginning of a substring. Add this value to string to get the address of that part.
rm_eo
The offset in string of the end of the substring.
— Data Type: regoff_t
regoff_t is an alias for another signed integer type. The fields of regmatch_t have type regoff_t.
The regmatch_t elements correspond to subexpressions positionally; the first element (index 1) records where the first subexpression matched, the second element records the second subexpression, and so on. The order of the subexpressions is the order in which they begin.
When you call regexec, you specify how long the matchptr array is, with the nmatch argument. This tells regexec how many elements to store. If the actual regular expression has more than nmatch subexpressions, then you won't get offset information about the rest of them. But this doesn't alter whether the pattern matches a particular string or not.
If you don't want regexec to return any information about where the subexpressions matched, you can either supply 0 for nmatch, or use the flag REG_NOSUB when you compile the pattern with regcomp.
#include <stdio.h> #include <sys/types.h> #include <stdlib.h> #include <string.h> #include <regex.h> /* 取子串的函数 */ static char* substr(const char*str, unsigned start, unsigned end) { unsigned n = end - start; static char stbuf[256]; strncpy(stbuf, str + start, n); stbuf[n] = 0; return stbuf; } /* 主程序 */ int main(int argc, char** argv) { char * pattern; int x, z, lno = 0, cflags = 0; char ebuf[128], lbuf[256]; regex_t reg; regmatch_t pm[10]; const size_t nmatch = 10; /* 编译正则表达式*/ pattern = argv[1]; z = regcomp(®, pattern, cflags); if (z != 0){ regerror(z, ®, ebuf, sizeof(ebuf)); fprintf(stderr, "%s: pattern '%s' \n", ebuf, pattern); return 1; } /* 逐行处理输入的数据 */ while(fgets(lbuf, sizeof(lbuf), stdin)) { ++lno; if ((z = strlen(lbuf)) >0 && lbuf[z-1] == '\n') lbuf[z - 1] = 0; /* 对每一行应用正则表达式进行匹配 */ z = regexec(®, lbuf, nmatch, pm, 0); if (z == REG_NOMATCH) continue; else if (z != 0) { regerror(z, ®, ebuf, sizeof(ebuf)); fprintf(stderr, "%s: regcom('%s')\n", ebuf, lbuf); return 2; } /* 输出处理结果 */ for (x = 0; x < nmatch && pm[x].rm_so != -1; ++ x) { if (!x) printf("%04d: %s\n", lno, lbuf); printf(" $%d='%s'\n", x, substr(lbuf, pm[x].rm_so, pm[x].rm_eo)); } } /* 释放正则表达式 */ regfree(®); return 0; } |
原帖由 fwizard 于 2004-4-13 22:22 发表
看到大家讨论这方面的东西,作点贡献聊表各位高手对这个版快的无私奉献
如果用户熟悉Linux下的sed、awk、grep或vi,那么对正则表达式这一概念肯定不会陌生。由于它可以极大地简化处理字符串时的复杂 ...
原帖由 yecheng_110 于 2008-1-1 00:20 发表
似乎这里有误
应该是以下的解释才对
字符串string中会有多处与给定的正则表达式相匹配,应该是匹配第一个就结束了,pmatch保存的应该是用‘()’括起来的Subexpression的位置
原帖由 ExclusivePig 于 2009-9-15 14:44 发表
跪求高手继续解释。。。我目前在用prce库,但总是只能匹配一个结果,对于多个匹配无法得到,我用的是pcre_compile,和prce_exec
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |