C를 사용하는 데 익숙하지 않으며 PCRE를 사용하여 일치해야합니다.
다음 내 소스 코드의 샘플입니다 :이 데모에서PCRE를 사용하여 모든 성냥 그룹을 얻으려면 어떻게해야합니까?
int test2()
{
const char *error;
int erroffset;
pcre *re;
int rc;
int i;
int ovector[OVECCOUNT];
char *regex = "From:([^@]+)@([^\r]+)";
char str[] = "From:[email protected]\r\n"\
"From:[email protected]\r\n"\
"From:[email protected]\r\n";
re = pcre_compile (
regex, /* the pattern */
0, /* default options */
&error, /* for error message */
&erroffset, /* for error offset */
0); /* use default character tables */
if (!re) {
printf("pcre_compile failed (offset: %d), %s\n", erroffset, error);
return -1;
}
rc = pcre_exec (
re, /* the compiled pattern */
0, /* no extra data - pattern was not studied */
str, /* the string to match */
strlen(str), /* the length of the string */
0, /* start at offset 0 in the subject */
0, /* default options */
ovector, /* output vector for substring information */
OVECCOUNT); /* number of elements in the output vector */
if (rc < 0) {
switch (rc) {
case PCRE_ERROR_NOMATCH:
printf("String didn't match");
break;
default:
printf("Error while matching: %d\n", rc);
break;
}
free(re);
return -1;
}
for (i = 0; i < rc; i++) {
printf("%2d: %.*s\n", i, ovector[2*i+1] - ovector[2*i], str + ovector[2*i]);
}
}
의 출력은 다음과 같습니다
0: From:[email protected]
1: regular.expressions
2: example.com
내가 출력에 모두를 원하는 성냥; 내가 어떻게 할 수 있니?
정규 표현식을 사용하지 말고 실제 파서를 사용하십시오. mail protocoal은 일반 우편함 주소 그 이상을 허용합니다. – Gumbo
이것은 pcre의 데모이며, 그룹 일치에 pcre를 사용하는 방법을 알고 싶습니다. 의견을 보내 주셔서 감사합니다. – tbmvp
이 게시물을 참조해야합니다 : http://stackoverflow.com/questions/7785557/pcre-match-all-groups-in-c – soulmachine