我想使用PCRE C库递归地匹配一个组。
例如
pattern = "(\d,)"
subject = "5,6,3,2,"
OVECCOUNT = 30
pcrePtr = pcre_compile(pattern, 0, &error, &erroffset, NULL);
rc = pcre_exec(pcrePtr, NULL, subject, (int)strlen(subject),
0, 0, ovector, OVECCOUNT);
rc是-1。。
如何匹配所有组,以便匹配为“5”、“6”、“3”、“2”
类似地,PHP的
preg_match_all
会解析整个字符串,直到主题结束。。。 最佳答案
试试这个:
pcre *myregexp;
const char *error;
int erroroffset;
int offsetcount;
int offsets[(0+1)*3]; // (max_capturing_groups+1)*3
myregexp = pcre_compile("\\d,", 0, &error, &erroroffset, NULL);
if (myregexp != NULL) {
offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, 0, offsets, (0+1)*3);
while (offsetcount > 0) {
// match offset = offsets[0];
// match length = offsets[1] - offsets[0];
if (pcre_get_substring(subject, &offsets, offsetcount, 0, &result) >= 0) {
// Do something with match we just stored into result
}
offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, offsets[1], offsets, (0+1)*3);
}
} else {
// Syntax error in the regular expression at erroroffset
}
我相信这些评论是不言而喻的?
关于c - pcre匹配C中的所有组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7785557/