6 gcc -Wall pcredemo.c -I/opt/local/include -L/opt/local/lib \
7 -R/opt/local/lib -lpcre
10 #define OVECCOUNT 30 /* should be a multiple of 3 */
12 int main(int argc, char **argv)
17 int ovector[OVECCOUNT];
22 printf("Two arguments required: a regex and a subject string\n");
26 /* Compile the regular expression in the first argument */
29 argv[1], /* the pattern */
30 0, /* default options */
31 &error, /* for error message */
32 &erroffset, /* for error offset */
33 NULL); /* use default character tables */
35 /* Compilation failed: print the error message and exit */
39 printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
43 /* Compilation succeeded: match the subject in the second argument */
46 re, /* the compiled pattern */
47 NULL, /* no extra data - we didn't study the pattern */
48 argv[2], /* the subject string */
49 (int)strlen(argv[2]), /* the length of the subject */
50 0, /* start at offset 0 in the subject */
51 0, /* default options */
52 ovector, /* output vector for substring information */
53 OVECCOUNT); /* number of elements in the output vector */
55 /* Matching failed: handle error cases */
61 case PCRE_ERROR_NOMATCH: printf("No match\n"); break;
63 Handle other special cases if you like
65 default: printf("Matching error %d\n", rc); break;
72 printf("Match succeeded\n");
74 /* The output vector wasn't big enough */
79 printf("ovector only has room for %d captured substrings\n", rc - 1);
82 /* Show substrings stored in the output vector */
84 for (i = 0; i < rc; i++)
86 char *substring_start = argv[2] + ovector[2*i];
87 int substring_length = ovector[2*i+1] - ovector[2*i];
88 printf("%2d: %.*s\n", i, substring_length, substring_start);