1
PCRE를 일부 정규식 파싱에 사용하고 있으며 특정 패턴의 단어를 검색해야합니다 (단어의 문자열에있는 모든 단어를 쉼표로 구분 해 봅시다). 문자열 벡터로 변환합니다.PCRE 정규 표현식에서 임의의 수의 단어를 문자열로 맞추기
어떻게해야할까요?
PCRE를 일부 정규식 파싱에 사용하고 있으며 특정 패턴의 단어를 검색해야합니다 (단어의 문자열에있는 모든 단어를 쉼표로 구분 해 봅시다). 문자열 벡터로 변환합니다.PCRE 정규 표현식에서 임의의 수의 단어를 문자열로 맞추기
어떻게해야할까요?
,하지만 난 서둘러입니다 ...
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];
}
}
std::string wordstring = "w1, w2, w3";
std::string word;
pcrecpp::StringPiece inp_w(wordstring);
pcrecpp::RE w_re("(\\S+),?\\s*");
std::vector outwords;
while (w_re.FindAndConsume(&inp_w, &word)) {
outwords.push_back(word);
}
는이 테스트 적이 있습니까? 쉼표를 마치 단어의 일부처럼 짝을 맞추기를 기대합니다. [ "w1", "w2", "w3"] –