我希望使用PCRE C库递归匹配一个组。
e.g。
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
解析整个字符串,直到主题结束......
答案 0 :(得分:0)
试试这个:
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
}
我认为这些评论是不言自明的?
答案 1 :(得分:0)
我使用strtok的任何方式,因为“,”在每组之后重复...
欢迎使用pcre的解决方案......