2011-10-16 44 views
1

我想使用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整個字符串,直到主體的結尾......

+0

您環圍繞'pcre_exec()',調整起始位置在每次迭代後適當。 –

+0

有沒有辦法讓它在一個鏡頭? – Rahul

+0

不是沒有寫函數來爲你做循環。 –

回答

0

任何方式我用的strtok自「」爲一組完成後重複使用.. PCRE

溶液歡迎....

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 
} 

我相信評論是自我解釋?

+0

它不起作用.... – Rahul

+0

@Rahul您使用哪種版本的PCRE? – FailedDev

+0

PCRE版本是8.10 – Rahul