pcre匹配C中的所有组
我想使用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 }
我相信这些评论是不言自明的?
自从“,”之后我用strtok的任何方式都在每组之后重复……
使用pcre的解决方案受到欢迎….