4 |
|
|
5 |
/* This is a demonstration program to illustrate the most straightforward ways |
/* This is a demonstration program to illustrate the most straightforward ways |
6 |
of calling the PCRE regular expression library from a C program. See the |
of calling the PCRE regular expression library from a C program. See the |
7 |
pcresample documentation for a short discussion. |
pcresample documentation for a short discussion ("man pcresample" if you have |
8 |
|
the PCRE man pages installed). |
9 |
|
|
10 |
Compile thuswise: |
In Unix-like environments, if PCRE is installed in your standard system |
11 |
gcc -Wall pcredemo.c -I/opt/local/include -L/opt/local/lib \ |
libraries, you should be able to compile this program using this command: |
|
-R/opt/local/lib -lpcre |
|
12 |
|
|
13 |
Replace "/opt/local/include" and "/opt/local/lib" with wherever the include and |
gcc -Wall pcredemo.c -lpcre -o pcredemo |
14 |
|
|
15 |
|
If PCRE is not installed in a standard place, it is likely to be installed with |
16 |
|
support for the pkg-config mechanism. If you have pkg-config, you can compile |
17 |
|
this program using this command: |
18 |
|
|
19 |
|
gcc -Wall pcredemo.c `pkg-config --cflags --libs libpcre` -o pcredemo |
20 |
|
|
21 |
|
If you do not have pkg-config, you may have to use this: |
22 |
|
|
23 |
|
gcc -Wall pcredemo.c -I/usr/local/include -L/usr/local/lib \ |
24 |
|
-R/usr/local/lib -lpcre -o pcredemo |
25 |
|
|
26 |
|
Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and |
27 |
library files for PCRE are installed on your system. Only some operating |
library files for PCRE are installed on your system. Only some operating |
28 |
systems (e.g. Solaris) use the -R option. |
systems (e.g. Solaris) use the -R option. |
|
*/ |
|
29 |
|
|
30 |
|
Building under Windows: |
31 |
|
|
32 |
|
If you want to statically link this program against a non-dll .a file, you must |
33 |
|
define PCRE_STATIC before including pcre.h, otherwise the pcre_malloc() and |
34 |
|
pcre_free() exported functions will be declared __declspec(dllimport), with |
35 |
|
unwanted results. So in this environment, uncomment the following line. */ |
36 |
|
|
37 |
|
/* #define PCRE_STATIC */ |
38 |
|
|
39 |
#include <stdio.h> |
#include <stdio.h> |
40 |
#include <string.h> |
#include <string.h> |
59 |
int rc, i; |
int rc, i; |
60 |
|
|
61 |
|
|
62 |
/************************************************************************* |
/************************************************************************** |
63 |
* 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 * |
64 |
* the moment, "-g" to request repeated matching to find all occurrences, * |
* the moment, "-g" to request repeated matching to find all occurrences, * |
65 |
* 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 * |
66 |
* present. Apart from that, there must be exactly two arguments. * |
* if the -g option is present. Apart from that, there must be exactly two * |
67 |
*************************************************************************/ |
* arguments. * |
68 |
|
**************************************************************************/ |
69 |
|
|
70 |
find_all = 0; |
find_all = 0; |
71 |
for (i = 1; i < argc; i++) |
for (i = 1; i < argc; i++) |
111 |
|
|
112 |
/************************************************************************* |
/************************************************************************* |
113 |
* 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 * |
114 |
* pattern match against the subject string. This just does ONE match. If * |
* pattern match against the subject string. This does just ONE match. If * |
115 |
* further matching is needed, it will be done below. * |
* further matching is needed, it will be done below. * |
116 |
*************************************************************************/ |
*************************************************************************/ |
117 |
|
|
137 |
*/ |
*/ |
138 |
default: printf("Matching error %d\n", rc); break; |
default: printf("Matching error %d\n", rc); break; |
139 |
} |
} |
140 |
|
pcre_free(re); /* Release memory used for the compiled pattern */ |
141 |
return 1; |
return 1; |
142 |
} |
} |
143 |
|
|
148 |
|
|
149 |
/************************************************************************* |
/************************************************************************* |
150 |
* We have found the first match within the subject string. If the output * |
* We have found the first match within the subject string. If the output * |
151 |
* vector wasn't big enough, set its size to the maximum. Then output any * |
* vector wasn't big enough, say so. Then output any substrings that were * |
152 |
* substrings that were captured. * |
* captured. * |
153 |
*************************************************************************/ |
*************************************************************************/ |
154 |
|
|
155 |
/* The output vector wasn't big enough */ |
/* The output vector wasn't big enough */ |
171 |
} |
} |
172 |
|
|
173 |
|
|
174 |
/************************************************************************* |
/************************************************************************** |
175 |
* That concludes the basic part of this demonstration program. We have * |
* That concludes the basic part of this demonstration program. We have * |
176 |
* compiled a pattern, and performed a single match. The code that follows* |
* compiled a pattern, and performed a single match. The code that follows * |
177 |
* first shows how to access named substrings, and then how to code for * |
* shows first how to access named substrings, and then how to code for * |
178 |
* repeated matches on the same subject. * |
* repeated matches on the same subject. * |
179 |
*************************************************************************/ |
**************************************************************************/ |
180 |
|
|
181 |
/* 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 |
182 |
we have to extract the count of named parentheses from the pattern. */ |
we have to extract the count of named parentheses from the pattern. */ |
233 |
* * |
* * |
234 |
* If the previous match WAS for an empty string, we can't do that, as it * |
* If the previous match WAS for an empty string, we can't do that, as it * |
235 |
* would lead to an infinite loop. Instead, a special call of pcre_exec() * |
* would lead to an infinite loop. Instead, a special call of pcre_exec() * |
236 |
* is made with the PCRE_NOTEMPTY and PCRE_ANCHORED flags set. The first * |
* is made with the PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED flags set. * |
237 |
* of these tells PCRE that an empty string is not a valid match; other * |
* The first of these tells PCRE that an empty string at the start of the * |
238 |
* possibilities must be tried. The second flag restricts PCRE to one * |
* subject is not a valid match; other possibilities must be tried. The * |
239 |
* match attempt at the initial string position. If this match succeeds, * |
* second flag restricts PCRE to one match attempt at the initial string * |
240 |
* an alternative to the empty string match has been found, and we can * |
* position. If this match succeeds, an alternative to the empty string * |
241 |
* proceed round the loop. * |
* match has been found, and we can proceed round the loop. * |
242 |
*************************************************************************/ |
*************************************************************************/ |
243 |
|
|
244 |
if (!find_all) return 0; /* Finish unless -g was given */ |
if (!find_all) |
245 |
|
{ |
246 |
|
pcre_free(re); /* Release the memory used for the compiled pattern */ |
247 |
|
return 0; /* Finish unless -g was given */ |
248 |
|
} |
249 |
|
|
250 |
/* Loop for second and subsequent matches */ |
/* Loop for second and subsequent matches */ |
251 |
|
|
261 |
if (ovector[0] == ovector[1]) |
if (ovector[0] == ovector[1]) |
262 |
{ |
{ |
263 |
if (ovector[0] == subject_length) break; |
if (ovector[0] == subject_length) break; |
264 |
options = PCRE_NOTEMPTY | PCRE_ANCHORED; |
options = PCRE_NOTEMPTY_ATSTART | PCRE_ANCHORED; |
265 |
} |
} |
266 |
|
|
267 |
/* Run the next matching operation */ |
/* Run the next matching operation */ |
296 |
if (rc < 0) |
if (rc < 0) |
297 |
{ |
{ |
298 |
printf("Matching error %d\n", rc); |
printf("Matching error %d\n", rc); |
299 |
|
pcre_free(re); /* Release memory used for the compiled pattern */ |
300 |
return 1; |
return 1; |
301 |
} |
} |
302 |
|
|
337 |
} /* End of loop to find second and subsequent matches */ |
} /* End of loop to find second and subsequent matches */ |
338 |
|
|
339 |
printf("\n"); |
printf("\n"); |
340 |
|
pcre_free(re); /* Release memory used for the compiled pattern */ |
341 |
return 0; |
return 0; |
342 |
} |
} |
343 |
|
|