Non ho esperienza con l'uso di C, e ho bisogno di usare PCRE per ottenere le partite.
Ecco un esempio del mio codice sorgente:Come posso utilizzare PCRE per ottenere tutti i gruppi di incontri?
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]);
}
}
In questa demo, l'uscita è unica:
0: From:[email protected]
1: regular.expressions
2: example.com
voglio uscita tutta la le partite; Come lo posso fare?
Non utilizzare un'espressione regolare ma un parser reale. Il protocoal di posta consente più di un semplice indirizzo di posta elettronica. – Gumbo
Questa è una demo per pcre, voglio solo sapere come usare pcre nei gruppi corrispondenti. Grazie per il tuo commento. – tbmvp
dovresti fare riferimento a questo post: http://stackoverflow.com/questions/7785557/pcre-match-all-groups-in-c – soulmachine