| 1 |
nigel |
53 |
#include <stdio.h> |
| 2 |
|
|
#include <string.h> |
| 3 |
|
|
#include <pcre.h> |
| 4 |
|
|
|
| 5 |
|
|
/* Compile thuswise: |
| 6 |
|
|
gcc -Wall pcredemo.c -I/opt/local/include -L/opt/local/lib \ |
| 7 |
|
|
-R/opt/local/lib -lpcre |
| 8 |
|
|
*/ |
| 9 |
|
|
|
| 10 |
|
|
#define OVECCOUNT 30 /* should be a multiple of 3 */ |
| 11 |
|
|
|
| 12 |
|
|
int main(int argc, char **argv) |
| 13 |
|
|
{ |
| 14 |
|
|
pcre *re; |
| 15 |
|
|
const char *error; |
| 16 |
|
|
int erroffset; |
| 17 |
|
|
int ovector[OVECCOUNT]; |
| 18 |
|
|
int rc, i; |
| 19 |
|
|
|
| 20 |
|
|
if (argc != 3) |
| 21 |
|
|
{ |
| 22 |
|
|
printf("Two arguments required: a regex and a subject string\n"); |
| 23 |
|
|
return 1; |
| 24 |
|
|
} |
| 25 |
|
|
|
| 26 |
|
|
/* Compile the regular expression in the first argument */ |
| 27 |
|
|
|
| 28 |
|
|
re = pcre_compile( |
| 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 */ |
| 34 |
|
|
|
| 35 |
|
|
/* Compilation failed: print the error message and exit */ |
| 36 |
|
|
|
| 37 |
|
|
if (re == NULL) |
| 38 |
|
|
{ |
| 39 |
|
|
printf("PCRE compilation failed at offset %d: %s\n", erroffset, error); |
| 40 |
|
|
return 1; |
| 41 |
|
|
} |
| 42 |
|
|
|
| 43 |
|
|
/* Compilation succeeded: match the subject in the second argument */ |
| 44 |
|
|
|
| 45 |
|
|
rc = pcre_exec( |
| 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 */ |
| 54 |
|
|
|
| 55 |
|
|
/* Matching failed: handle error cases */ |
| 56 |
|
|
|
| 57 |
|
|
if (rc < 0) |
| 58 |
|
|
{ |
| 59 |
|
|
switch(rc) |
| 60 |
|
|
{ |
| 61 |
|
|
case PCRE_ERROR_NOMATCH: printf("No match\n"); break; |
| 62 |
|
|
/* |
| 63 |
|
|
Handle other special cases if you like |
| 64 |
|
|
*/ |
| 65 |
|
|
default: printf("Matching error %d\n", rc); break; |
| 66 |
|
|
} |
| 67 |
|
|
return 1; |
| 68 |
|
|
} |
| 69 |
|
|
|
| 70 |
|
|
/* Match succeded */ |
| 71 |
|
|
|
| 72 |
|
|
printf("Match succeeded\n"); |
| 73 |
|
|
|
| 74 |
|
|
/* The output vector wasn't big enough */ |
| 75 |
|
|
|
| 76 |
|
|
if (rc == 0) |
| 77 |
|
|
{ |
| 78 |
|
|
rc = OVECCOUNT/3; |
| 79 |
|
|
printf("ovector only has room for %d captured substrings\n", rc - 1); |
| 80 |
|
|
} |
| 81 |
|
|
|
| 82 |
|
|
/* Show substrings stored in the output vector */ |
| 83 |
|
|
|
| 84 |
|
|
for (i = 0; i < rc; i++) |
| 85 |
|
|
{ |
| 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); |
| 89 |
|
|
} |
| 90 |
|
|
|
| 91 |
|
|
return 0; |
| 92 |
|
|
} |
| 93 |
|
|
|
| 94 |
|
|
|