正则表达式用C - 比赛组 [英] Regexp in C - match group
问题描述
我一直挣扎在C常规EX pressions(只 /usr/include/regex.h
)。
I've been struggling with regular expressions in C (just /usr/include/regex.h
).
我有(比方说)数百名正则表达式的,其中一个可以匹配输入字符串。
与之相匹配的数百DO-而内,如果突破不匹配,并要另:目前我做的(生成它实际上)这样。一个接一个:
I have (let's say) hundreds of regexps and one of them can match input string. Currently I'm doing it (generating it actually) like this: hundreds of do-while with match inside, break if not matching and going to another. One by one:
do {
if ( regex_match(str, my_regex1) != MY_REGEX_SUCCESS ) DO_FAIL; //break
...
if ( sscanf(str, " %d.%d.%d.%d / %d ", &___ip1, &___ip2, &___ip3, &___ip4, &___pref) != 5 ) DO_FAIL; //break
...
} while (0);
do {
if ( regex_match(str, my_regex2) != MY_REGEX_SUCCESS ) DO_FAIL; //break
...
...
} while (0);
do {
if ( regex_match(str, my_regex3) != MY_REGEX_SUCCESS ) DO_FAIL; //break
...
...
} while (0);
我想拥有的是一样的东西:
What I'd like to have is something like:
const char * match1 = "^([[:space:]]*)([$]([._a-zA-Z0-9-]{0,118})?[._a-zA-Z0-9])([[:space:]]*)$";
const char * match2 = "^([[:space:]]*)(target|origin)([[:space:]]*):([[:space:]]*)([$]([._a-zA-Z0-9-]{0,118})?[._a-zA-Z0-9])([[:space:]]*):([[:space:]]*)\\*([[:space:]]*)$";
const char * match3 = "^([[:space:]]*)(target|origin)([[:space:]]*):([[:space:]]*)([$]([._a-zA-Z0-9-]{0,118})?[._a-zA-Z0-9])([[:space:]]*)/([[:space:]]*)(([0-2]?[0-9])|(3[0-2]))([[:space:]]*):([[:space:]]*)(([1-9][0-9]{0,3})|([1-5][0-9]{4})|(6[0-4][0-9]{3})|(65[0-4][0-9]{2})|(655[0-2][0-9])|(6553[0-5]))([[:space:]]*)$";
char * my_match;
asprintf(&my_match, "(%s)|(%s)|(%s)", match1, match2, match3);
int num_gr = give_me_number_of_regex_group(str, my_match)
switch (num_gr) {
...
}
和没有一个想法如何做到这一点...
and don't have an idea how to do that...
有什么建议?结果
谢谢!
Any suggestions?
Thanks!
推荐答案
我假设你的 regex_match
是的 regcomp
和 regexec
。要启用分组,您需要调用 regcomp
与 REG_EXTENDED
标记,但没有 REG_NOSUB
标记(第三个参数)。
I assume your regex_match
is some combination of regcomp
and regexec
. To enable grouping, you need to call regcomp
with the REG_EXTENDED
flag, but without the REG_NOSUB
flag (in the third argument).
regex_t compiled;
regcomp(&compiled, "(match1)|(match2)|(match3)", REG_EXTENDED);
然后各组分配空间。基团的数目被存储在 compiled.re_nsub
。通过这个号码 regexec
:
size_t ngroups = compiled.re_nsub + 1;
regmatch_t *groups = malloc(ngroups * sizeof(regmatch_t));
regexec(&compiled, str, ngroups, groups, 0);
现在,第一个无效的团体就是在这两个公司 rm_so
和 rm_eo
字段值-1
Now, the first invalid group is the one with a -1 value in both its rm_so
and rm_eo
fields:
size_t nmatched;
for (nmatched = 0; nmatched < ngroups; nmatched++)
if (groups[nmatched].rm_so == (size_t)(-1))
break;
nmatched
是匹配的括号的SUBEX pressions(组)的数量。添加您自己的错误检查。
nmatched
is the number of parenthesized subexpressions (groups) matched. Add your own error checking.
这篇关于正则表达式用C - 比赛组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!