3 <title>pcredemo specification</title>
5 <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6 <h1>pcredemo man page</h1>
8 Return to the <a href="index.html">PCRE index page</a>.
11 This page is part of the PCRE HTML documentation. It was generated automatically
12 from the original man page. If there is any nonsense in it, please consult the
13 man page, in case the conversion went wrong.
18 /*************************************************
19 * PCRE DEMONSTRATION PROGRAM *
20 *************************************************/
22 /* This is a demonstration program to illustrate the most straightforward ways
23 of calling the PCRE regular expression library from a C program. See the
24 pcresample documentation for a short discussion ("man pcresample" if you have
25 the PCRE man pages installed).
27 In Unix-like environments, if PCRE is installed in your standard system
28 libraries, you should be able to compile this program using this command:
30 gcc -Wall pcredemo.c -lpcre -o pcredemo
32 If PCRE is not installed in a standard place, it is likely to be installed with
33 support for the pkg-config mechanism. If you have pkg-config, you can compile
34 this program using this command:
36 gcc -Wall pcredemo.c `pkg-config --cflags --libs libpcre` -o pcredemo
38 If you do not have pkg-config, you may have to use this:
40 gcc -Wall pcredemo.c -I/usr/local/include -L/usr/local/lib \
41 -R/usr/local/lib -lpcre -o pcredemo
43 Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and
44 library files for PCRE are installed on your system. Only some operating
45 systems (e.g. Solaris) use the -R option.
47 Building under Windows:
49 If you want to statically link this program against a non-dll .a file, you must
50 define PCRE_STATIC before including pcre.h, otherwise the pcre_malloc() and
51 pcre_free() exported functions will be declared __declspec(dllimport), with
52 unwanted results. So in this environment, uncomment the following line. */
54 /* #define PCRE_STATIC */
56 #include <stdio.h>
57 #include <string.h>
58 #include <pcre.h>
60 #define OVECCOUNT 30 /* should be a multiple of 3 */
63 int main(int argc, char **argv)
69 unsigned char *name_table;
74 int ovector[OVECCOUNT];
79 /**************************************************************************
80 * First, sort out the command line. There is only one possible option at *
81 * the moment, "-g" to request repeated matching to find all occurrences, *
82 * like Perl's /g option. We set the variable find_all to a non-zero value *
83 * if the -g option is present. Apart from that, there must be exactly two *
85 **************************************************************************/
88 for (i = 1; i < argc; i++)
90 if (strcmp(argv[i], "-g") == 0) find_all = 1;
94 /* After the options, we require exactly two arguments, which are the pattern,
95 and the subject string. */
99 printf("Two arguments required: a regex and a subject string\n");
105 subject_length = (int)strlen(subject);
108 /*************************************************************************
109 * Now we are going to compile the regular expression pattern, and handle *
110 * and errors that are detected. *
111 *************************************************************************/
114 pattern, /* the pattern */
115 0, /* default options */
116 &error, /* for error message */
117 &erroffset, /* for error offset */
118 NULL); /* use default character tables */
120 /* Compilation failed: print the error message and exit */
124 printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
129 /*************************************************************************
130 * If the compilation succeeded, we call PCRE again, in order to do a *
131 * pattern match against the subject string. This does just ONE match. If *
132 * further matching is needed, it will be done below. *
133 *************************************************************************/
136 re, /* the compiled pattern */
137 NULL, /* no extra data - we didn't study the pattern */
138 subject, /* the subject string */
139 subject_length, /* the length of the subject */
140 0, /* start at offset 0 in the subject */
141 0, /* default options */
142 ovector, /* output vector for substring information */
143 OVECCOUNT); /* number of elements in the output vector */
145 /* Matching failed: handle error cases */
151 case PCRE_ERROR_NOMATCH: printf("No match\n"); break;
153 Handle other special cases if you like
155 default: printf("Matching error %d\n", rc); break;
157 pcre_free(re); /* Release memory used for the compiled pattern */
163 printf("\nMatch succeeded at offset %d\n", ovector[0]);
166 /*************************************************************************
167 * We have found the first match within the subject string. If the output *
168 * vector wasn't big enough, say so. Then output any substrings that were *
170 *************************************************************************/
172 /* The output vector wasn't big enough */
177 printf("ovector only has room for %d captured substrings\n", rc - 1);
180 /* Show substrings stored in the output vector by number. Obviously, in a real
181 application you might want to do things other than print them. */
183 for (i = 0; i < rc; i++)
185 char *substring_start = subject + ovector[2*i];
186 int substring_length = ovector[2*i+1] - ovector[2*i];
187 printf("%2d: %.*s\n", i, substring_length, substring_start);
191 /**************************************************************************
192 * That concludes the basic part of this demonstration program. We have *
193 * compiled a pattern, and performed a single match. The code that follows *
194 * shows first how to access named substrings, and then how to code for *
195 * repeated matches on the same subject. *
196 **************************************************************************/
198 /* See if there are any named substrings, and if so, show them by name. First
199 we have to extract the count of named parentheses from the pattern. */
202 re, /* the compiled pattern */
203 NULL, /* no extra data - we didn't study the pattern */
204 PCRE_INFO_NAMECOUNT, /* number of named substrings */
205 &namecount); /* where to put the answer */
207 if (namecount <= 0) printf("No named substrings\n"); else
209 unsigned char *tabptr;
210 printf("Named substrings\n");
212 /* Before we can access the substrings, we must extract the table for
213 translating names to numbers, and the size of each entry in the table. */
216 re, /* the compiled pattern */
217 NULL, /* no extra data - we didn't study the pattern */
218 PCRE_INFO_NAMETABLE, /* address of the table */
219 &name_table); /* where to put the answer */
222 re, /* the compiled pattern */
223 NULL, /* no extra data - we didn't study the pattern */
224 PCRE_INFO_NAMEENTRYSIZE, /* size of each entry in the table */
225 &name_entry_size); /* where to put the answer */
227 /* Now we can scan the table and, for each entry, print the number, the name,
228 and the substring itself. */
231 for (i = 0; i < namecount; i++)
233 int n = (tabptr[0] << 8) | tabptr[1];
234 printf("(%d) %*s: %.*s\n", n, name_entry_size - 3, tabptr + 2,
235 ovector[2*n+1] - ovector[2*n], subject + ovector[2*n]);
236 tabptr += name_entry_size;
241 /*************************************************************************
242 * If the "-g" option was given on the command line, we want to continue *
243 * to search for additional matches in the subject string, in a similar *
244 * way to the /g option in Perl. This turns out to be trickier than you *
245 * might think because of the possibility of matching an empty string. *
246 * What happens is as follows: *
248 * If the previous match was NOT for an empty string, we can just start *
249 * the next match at the end of the previous one. *
251 * If the previous match WAS for an empty string, we can't do that, as it *
252 * would lead to an infinite loop. Instead, a special call of pcre_exec() *
253 * is made with the PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED flags set. *
254 * The first of these tells PCRE that an empty string at the start of the *
255 * subject is not a valid match; other possibilities must be tried. The *
256 * second flag restricts PCRE to one match attempt at the initial string *
257 * position. If this match succeeds, an alternative to the empty string *
258 * match has been found, and we can proceed round the loop. *
259 *************************************************************************/
263 pcre_free(re); /* Release the memory used for the compiled pattern */
264 return 0; /* Finish unless -g was given */
267 /* Loop for second and subsequent matches */
271 int options = 0; /* Normally no options */
272 int start_offset = ovector[1]; /* Start at end of previous match */
274 /* If the previous match was for an empty string, we are finished if we are
275 at the end of the subject. Otherwise, arrange to run another match at the
276 same point to see if a non-empty match can be found. */
278 if (ovector[0] == ovector[1])
280 if (ovector[0] == subject_length) break;
281 options = PCRE_NOTEMPTY_ATSTART | PCRE_ANCHORED;
284 /* Run the next matching operation */
287 re, /* the compiled pattern */
288 NULL, /* no extra data - we didn't study the pattern */
289 subject, /* the subject string */
290 subject_length, /* the length of the subject */
291 start_offset, /* starting offset in the subject */
292 options, /* options */
293 ovector, /* output vector for substring information */
294 OVECCOUNT); /* number of elements in the output vector */
296 /* This time, a result of NOMATCH isn't an error. If the value in "options"
297 is zero, it just means we have found all possible matches, so the loop ends.
298 Otherwise, it means we have failed to find a non-empty-string match at a
299 point where there was a previous empty-string match. In this case, we do what
300 Perl does: advance the matching position by one, and continue. We do this by
301 setting the "end of previous match" offset, because that is picked up at the
302 top of the loop as the point at which to start again. */
304 if (rc == PCRE_ERROR_NOMATCH)
306 if (options == 0) break;
307 ovector[1] = start_offset + 1;
308 continue; /* Go round the loop again */
311 /* Other matching errors are not recoverable. */
315 printf("Matching error %d\n", rc);
316 pcre_free(re); /* Release memory used for the compiled pattern */
322 printf("\nMatch succeeded again at offset %d\n", ovector[0]);
324 /* The match succeeded, but the output vector wasn't big enough. */
329 printf("ovector only has room for %d captured substrings\n", rc - 1);
332 /* As before, show substrings stored in the output vector by number, and then
333 also any named substrings. */
335 for (i = 0; i < rc; i++)
337 char *substring_start = subject + ovector[2*i];
338 int substring_length = ovector[2*i+1] - ovector[2*i];
339 printf("%2d: %.*s\n", i, substring_length, substring_start);
342 if (namecount <= 0) printf("No named substrings\n"); else
344 unsigned char *tabptr = name_table;
345 printf("Named substrings\n");
346 for (i = 0; i < namecount; i++)
348 int n = (tabptr[0] << 8) | tabptr[1];
349 printf("(%d) %*s: %.*s\n", n, name_entry_size - 3, tabptr + 2,
350 ovector[2*n+1] - ovector[2*n], subject + ovector[2*n]);
351 tabptr += name_entry_size;
354 } /* End of loop to find second and subsequent matches */
357 pcre_free(re); /* Release memory used for the compiled pattern */
361 /* End of pcredemo.c */
363 Return to the <a href="index.html">PCRE index page</a>.