7 |
pcresample documentation for a short discussion. |
pcresample documentation for a short discussion. |
8 |
|
|
9 |
Compile thuswise: |
Compile thuswise: |
10 |
gcc -Wall pcredemo.c -I/opt/local/include -L/opt/local/lib \ |
gcc -Wall pcredemo.c -I/usr/local/include -L/usr/local/lib \ |
11 |
-R/opt/local/lib -lpcre |
-R/usr/local/lib -lpcre |
12 |
|
|
13 |
Replace "/opt/local/include" and "/opt/local/lib" with wherever the include and |
Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and |
14 |
library files for PCRE are installed on your system. Only some operating |
library files for PCRE are installed on your system. You don't need -I and -L |
15 |
|
if PCRE is installed in the standard system libraries. Only some operating |
16 |
systems (e.g. Solaris) use the -R option. |
systems (e.g. Solaris) use the -R option. |
17 |
*/ |
*/ |
18 |
|
|
40 |
int rc, i; |
int rc, i; |
41 |
|
|
42 |
|
|
43 |
/************************************************************************* |
/************************************************************************** |
44 |
* First, sort out the command line. There is only one possible option at * |
* First, sort out the command line. There is only one possible option at * |
45 |
* the moment, "-g" to request repeated matching to find all occurrences, * |
* the moment, "-g" to request repeated matching to find all occurrences, * |
46 |
* like Perl's /g option. We set the variable find_all non-zero if it is * |
* like Perl's /g option. We set the variable find_all to a non-zero value * |
47 |
* present. Apart from that, there must be exactly two arguments. * |
* if the -g option is present. Apart from that, there must be exactly two * |
48 |
*************************************************************************/ |
* arguments. * |
49 |
|
**************************************************************************/ |
50 |
|
|
51 |
find_all = 0; |
find_all = 0; |
52 |
for (i = 1; i < argc; i++) |
for (i = 1; i < argc; i++) |
92 |
|
|
93 |
/************************************************************************* |
/************************************************************************* |
94 |
* If the compilation succeeded, we call PCRE again, in order to do a * |
* If the compilation succeeded, we call PCRE again, in order to do a * |
95 |
* pattern match against the subject string. This just does ONE match. If * |
* pattern match against the subject string. This does just ONE match. If * |
96 |
* further matching is needed, it will be done below. * |
* further matching is needed, it will be done below. * |
97 |
*************************************************************************/ |
*************************************************************************/ |
98 |
|
|
118 |
*/ |
*/ |
119 |
default: printf("Matching error %d\n", rc); break; |
default: printf("Matching error %d\n", rc); break; |
120 |
} |
} |
121 |
|
pcre_free(re); /* Release memory used for the compiled pattern */ |
122 |
return 1; |
return 1; |
123 |
} |
} |
124 |
|
|
152 |
} |
} |
153 |
|
|
154 |
|
|
155 |
/************************************************************************* |
/************************************************************************** |
156 |
* That concludes the basic part of this demonstration program. We have * |
* That concludes the basic part of this demonstration program. We have * |
157 |
* compiled a pattern, and performed a single match. The code that follows* |
* compiled a pattern, and performed a single match. The code that follows * |
158 |
* first shows how to access named substrings, and then how to code for * |
* first shows how to access named substrings, and then how to code for * |
159 |
* repeated matches on the same subject. * |
* repeated matches on the same subject. * |
160 |
*************************************************************************/ |
**************************************************************************/ |
161 |
|
|
162 |
/* See if there are any named substrings, and if so, show them by name. First |
/* See if there are any named substrings, and if so, show them by name. First |
163 |
we have to extract the count of named parentheses from the pattern. */ |
we have to extract the count of named parentheses from the pattern. */ |
222 |
* proceed round the loop. * |
* proceed round the loop. * |
223 |
*************************************************************************/ |
*************************************************************************/ |
224 |
|
|
225 |
if (!find_all) return 0; /* Finish unless -g was given */ |
if (!find_all) |
226 |
|
{ |
227 |
|
pcre_free(re); /* Release the memory used for the compiled pattern */ |
228 |
|
return 0; /* Finish unless -g was given */ |
229 |
|
} |
230 |
|
|
231 |
/* Loop for second and subsequent matches */ |
/* Loop for second and subsequent matches */ |
232 |
|
|
277 |
if (rc < 0) |
if (rc < 0) |
278 |
{ |
{ |
279 |
printf("Matching error %d\n", rc); |
printf("Matching error %d\n", rc); |
280 |
|
pcre_free(re); /* Release memory used for the compiled pattern */ |
281 |
return 1; |
return 1; |
282 |
} |
} |
283 |
|
|
318 |
} /* End of loop to find second and subsequent matches */ |
} /* End of loop to find second and subsequent matches */ |
319 |
|
|
320 |
printf("\n"); |
printf("\n"); |
321 |
|
pcre_free(re); /* Release memory used for the compiled pattern */ |
322 |
return 0; |
return 0; |
323 |
} |
} |
324 |
|
|