我正在使用PCRE进行一些正则表达式解析,我需要在字符串中搜索特定模式的单词(比方说,用逗号分隔的字符串中的所有单词),然后将它们放入字符串 vector 中。
我将如何去做?
最佳答案
不好意思,我很着急...
pcre* re;
const char *error;
int erroffset;
char* subject = txt;
int ovector[3];
int subject_length = strlen(subject);
int rc = 0;
re = pcre_compile(
"\\w+", /* the pattern */
PCRE_CASELESS|PCRE_MULTILINE, /* default options */
&error, /* for error message */
&erroffset, /* for error offset */
NULL); /* use default character tables */
char* pofs = subject;
while ( rc >= 0 ) {
rc = pcre_exec(
re, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
subject, /* the subject string */
subject_length, /* the length of the subject */
0, /* start at offset 0 in the subject */
0, /* default options */
ovector, /* output vector for substring information */
3); /* number of elements in the output vector */
/*
if (rc < 0) {
switch(rc) {
case PCRE_ERROR_NOMATCH: printf("No match\n"); break;
// Handle other special cases if you like
default: printf("Matching error %d\n", rc); break;
}
pcre_free(re); // Release memory used for the compiled pattern
return;
}
*/
/* Match succeded */
if ( rc >= 0 ) {
pofs += ovector[1];
char *substring_start = subject + ovector[0];
// do something with the substring
int substring_length = ovector[1] - ovector[0];
subject = pofs;
subject_length -= ovector[1];
}
}
关于c++ - 将PCRE正则表达式中的任意数量的单词匹配为字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/937948/