1// ‐ A demonstration C program for PCRE2 ‐ //
2
3/*************************************************
4* PCRE2 DEMONSTRATION PROGRAM *
5*************************************************/
6
7/* This is a demonstration program to illustrate a straightforward way of
8using the PCRE2 regular expression library from a C program. See the
9pcre2sample documentation for a short discussion ("man pcre2sample" if you have
10the PCRE2 man pages installed). PCRE2 is a revised API for the library, and is
11incompatible with the original PCRE API.
12
13There are actually three libraries, each supporting a different code unit
14width. This demonstration program uses the 8‐bit library. The default is to
15process each code unit as a separate character, but if the pattern begins with
16"(*UTF)", both it and the subject are treated as UTF‐8 strings, where
17characters may occupy multiple code units.
18
19In Unix‐like environments, if PCRE2 is installed in your standard system
20libraries, you should be able to compile this program using this command:
21
22cc ‐Wall pcre2demo.c ‐lpcre2‐8 ‐o pcre2demo
23
24If PCRE2 is not installed in a standard place, it is likely to be installed
25with support for the pkg‐config mechanism. If you have pkg‐config, you can
26compile this program using this command:
27
28cc ‐Wall pcre2demo.c ‘pkg‐config ‐‐cflags ‐‐libs libpcre2‐8‘ ‐o pcre2demo
29
30If you do not have pkg‐config, you may have to use something like this:
31
32cc ‐Wall pcre2demo.c ‐I/usr/local/include ‐L/usr/local/lib \
33 ‐R/usr/local/lib ‐lpcre2‐8 ‐o pcre2demo
34
35Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and
36library files for PCRE2 are installed on your system. Only some operating
37systems (Solaris is one) use the ‐R option.
38
39Building under Windows:
40
41If you want to statically link this program against a non‐dll .a file, you must
42define PCRE2_STATIC before including pcre2.h, so in this environment, uncomment
43the following line. */
44
45/* #define PCRE2_STATIC */
46
47/* The PCRE2_CODE_UNIT_WIDTH macro must be defined before including pcre2.h.
48For a program that uses only one code unit width, setting it to 8, 16, or 32
49makes it possible to use generic function names such as pcre2_compile(). Note
50that just changing 8 to 16 (for example) is not sufficient to convert this
51program to process 16‐bit characters. Even in a fully 16‐bit environment, where
52string‐handling functions such as strcmp() and printf() work with 16‐bit
53characters, the code for handling the table of named substrings will still need
54to be modified. */
55
56#define PCRE2_CODE_UNIT_WIDTH 8
57
58#include <stdio.h>
59#include <string.h>
60#include <pcre2.h>
61
62
63/**************************************************************************
64* Here is the program. The API includes the concept of "contexts" for *
65* setting up unusual interface requirements for compiling and matching, *
66* such as custom memory managers and non‐standard newline definitions. *
67* This program does not do any of this, so it makes no use of contexts, *
68* always passing NULL where a context could be given. *
69**************************************************************************/
70
71int main(int argc, char **argv)
72{
73pcre2_code *re;
74PCRE2_SPTR pattern; /* PCRE2_SPTR is a pointer to unsigned code units of */
75PCRE2_SPTR subject; /* the appropriate width (in this case, 8 bits). */
76PCRE2_SPTR name_table;
77
78int crlf_is_newline;
79int errornumber;
80int find_all;
81int i;
82int rc;
83int utf8;
84
85uint32_t option_bits;
86uint32_t namecount;
87uint32_t name_entry_size;
88uint32_t newline;
89
90PCRE2_SIZE erroroffset;
91PCRE2_SIZE *ovector;
92PCRE2_SIZE subject_length;
93
94pcre2_match_data *match_data;
95
96
97/**************************************************************************
98* First, sort out the command line. There is only one possible option at *
99* the moment, "‐g" to request repeated matching to find all occurrences, *
100* like Perl’s /g option. We set the variable find_all to a non‐zero value *
101* if the ‐g option is present. *
102**************************************************************************/
103
104find_all = 0;
105for (i = 1; i < argc; i++)
106 {
107 if (strcmp(argv[i], "‐g") == 0) find_all = 1;
108 else if (argv[i][0] == ’‐’)
109 {
110 printf("Unrecognised option %s\n", argv[i]);
111 return 1;
112 }
113 else break;
114 }
115
116/* After the options, we require exactly two arguments, which are the pattern,
117and the subject string. */
118
119if (argc ‐ i != 2)
120 {
121 printf("Exactly two arguments required: a regex and a subject string\n");
122 return 1;
123 }
124
125/* Pattern and subject are char arguments, so they can be straightforwardly
126cast to PCRE2_SPTR because we are working in 8‐bit code units. The subject
127length is cast to PCRE2_SIZE for completeness, though PCRE2_SIZE is in fact
128defined to be size_t. */
129
130pattern = (PCRE2_SPTR)argv[i];
131subject = (PCRE2_SPTR)argv[i+1];
132subject_length = (PCRE2_SIZE)strlen((char *)subject);
133
134
135/*************************************************************************
136* Now we are going to compile the regular expression pattern, and handle *
137* any errors that are detected. *
138*************************************************************************/
139
140re = pcre2_compile(
141 pattern, /* the pattern */
142 PCRE2_ZERO_TERMINATED, /* indicates pattern is zero‐terminated */
143 0, /* default options */
144 &errornumber, /* for error number */
145 &erroroffset, /* for error offset */
146 NULL); /* use default compile context */
147
148/* Compilation failed: print the error message and exit. */
149
150if (re == NULL)
151 {
152 PCRE2_UCHAR buffer[256];
153 pcre2_get_error_message(errornumber, buffer, sizeof(buffer));
154 printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset,
155 buffer);
156 return 1;
157 }
158
159
160/*************************************************************************
161* If the compilation succeeded, we call PCRE2 again, in order to do a *
162* pattern match against the subject string. This does just ONE match. If *
163* further matching is needed, it will be done below. Before running the *
164* match we must set up a match_data block for holding the result. Using *
165* pcre2_match_data_create_from_pattern() ensures that the block is *
166* exactly the right size for the number of capturing parentheses in the *
167* pattern. If you need to know the actual size of a match_data block as *
168* a number of bytes, you can find it like this: *
169* *
170* PCRE2_SIZE match_data_size = pcre2_get_match_data_size(match_data); *
171*************************************************************************/
172
173match_data = pcre2_match_data_create_from_pattern(re, NULL);
174
175/* Now run the match. */
176
177rc = pcre2_match(
178 re, /* the compiled pattern */
179 subject, /* the subject string */
180 subject_length, /* the length of the subject */
181 0, /* start at offset 0 in the subject */
182 0, /* default options */
183 match_data, /* block for storing the result */
184 NULL); /* use default match context */
185
186/* Matching failed: handle error cases */
187
188if (rc < 0)
189 {
190 switch(rc)
191 {
192 case PCRE2_ERROR_NOMATCH: printf("No match\n"); break;
193 /*
194 Handle other special cases if you like
195 */
196 default: printf("Matching error %d\n", rc); break;
197 }
198 pcre2_match_data_free(match_data); /* Release memory used for the match */
199 pcre2_code_free(re); /* data and the compiled pattern. */
200 return 1;
201 }
202
203/* Match succeeded. Get a pointer to the output vector, where string offsets
204are stored. */
205
206ovector = pcre2_get_ovector_pointer(match_data);
207printf("Match succeeded at offset %d\n", (int)ovector[0]);
208
209
210/*************************************************************************
211* We have found the first match within the subject string. If the output *
212* vector wasn’t big enough, say so. Then output any substrings that were *
213* captured. *
214*************************************************************************/
215
216/* The output vector wasn’t big enough. This should not happen, because we used
217pcre2_match_data_create_from_pattern() above. */
218
219if (rc == 0)
220 printf("ovector was not big enough for all the captured substrings\n");
221
222/* Since release 10.38 PCRE2 has locked out the use of \K in lookaround
223assertions. However, there is an option to re‐enable the old behaviour. If that
224is set, it is possible to run patterns such as /(?=.\K)/ that use \K in an
225assertion to set the start of a match later than its end. In this demonstration
226program, we show how to detect this case, but it shouldn’t arise because the
227option is never set. */
228
229if (ovector[0] > ovector[1])
230 {
231 printf("\\K was used in an assertion to set the match start after its end.\n"
232 "From end to start the match was: %.*s\n", (int)(ovector[0] ‐ ovector[1]),
233 (char *)(subject + ovector[1]));
234 printf("Run abandoned\n");
235 pcre2_match_data_free(match_data);
236 pcre2_code_free(re);
237 return 1;
238 }
239
240/* Show substrings stored in the output vector by number. Obviously, in a real
241application you might want to do things other than print them. */
242
243for (i = 0; i < rc; i++)
244 {
245 PCRE2_SPTR substring_start = subject + ovector[2*i];
246 PCRE2_SIZE substring_length = ovector[2*i+1] ‐ ovector[2*i];
247 printf("%2d: %.*s\n", i, (int)substring_length, (char *)substring_start);
248 }
249
250
251/**************************************************************************
252* That concludes the basic part of this demonstration program. We have *
253* compiled a pattern, and performed a single match. The code that follows *
254* shows first how to access named substrings, and then how to code for *
255* repeated matches on the same subject. *
256**************************************************************************/
257
258/* See if there are any named substrings, and if so, show them by name. First
259we have to extract the count of named parentheses from the pattern. */
260
261(void)pcre2_pattern_info(
262 re, /* the compiled pattern */
263 PCRE2_INFO_NAMECOUNT, /* get the number of named substrings */
264 &namecount); /* where to put the answer */
265
266if (namecount == 0) printf("No named substrings\n"); else
267 {
268 PCRE2_SPTR tabptr;
269 printf("Named substrings\n");
270
271 /* Before we can access the substrings, we must extract the table for
272 translating names to numbers, and the size of each entry in the table. */
273
274 (void)pcre2_pattern_info(
275 re, /* the compiled pattern */
276 PCRE2_INFO_NAMETABLE, /* address of the table */
277 &name_table); /* where to put the answer */
278
279 (void)pcre2_pattern_info(
280 re, /* the compiled pattern */
281 PCRE2_INFO_NAMEENTRYSIZE, /* size of each entry in the table */
282 &name_entry_size); /* where to put the answer */
283
284 /* Now we can scan the table and, for each entry, print the number, the name,
285 and the substring itself. In the 8‐bit library the number is held in two
286 bytes, most significant first. */
287
288 tabptr = name_table;
289 for (i = 0; i < namecount; i++)
290 {
291 int n = (tabptr[0] << 8) | tabptr[1];
292 printf("(%d) %*s: %.*s\n", n, name_entry_size ‐ 3, tabptr + 2,
293 (int)(ovector[2*n+1] ‐ ovector[2*n]), subject + ovector[2*n]);
294 tabptr += name_entry_size;
295 }
296 }
297
298
299/*************************************************************************
300* If the "‐g" option was given on the command line, we want to continue *
301* to search for additional matches in the subject string, in a similar *
302* way to the /g option in Perl. This turns out to be trickier than you *
303* might think because of the possibility of matching an empty string. *
304* What happens is as follows: *
305* *
306* If the previous match was NOT for an empty string, we can just start *
307* the next match at the end of the previous one. *
308* *
309* If the previous match WAS for an empty string, we can’t do that, as it *
310* would lead to an infinite loop. Instead, a call of pcre2_match() is *
311* made with the PCRE2_NOTEMPTY_ATSTART and PCRE2_ANCHORED flags set. The *
312* first of these tells PCRE2 that an empty string at the start of the *
313* subject is not a valid match; other possibilities must be tried. The *
314* second flag restricts PCRE2 to one match attempt at the initial string *
315* position. If this match succeeds, an alternative to the empty string *
316* match has been found, and we can print it and proceed round the loop, *
317* advancing by the length of whatever was found. If this match does not *
318* succeed, we still stay in the loop, advancing by just one character. *
319* In UTF‐8 mode, which can be set by (*UTF) in the pattern, this may be *
320* more than one byte. *
321* *
322* However, there is a complication concerned with newlines. When the *
323* newline convention is such that CRLF is a valid newline, we must *
324* advance by two characters rather than one. The newline convention can *
325* be set in the regex by (*CR), etc.; if not, we must find the default. *
326*************************************************************************/
327
328if (!find_all) /* Check for ‐g */
329 {
330 pcre2_match_data_free(match_data); /* Release the memory that was used */
331 pcre2_code_free(re); /* for the match data and the pattern. */
332 return 0; /* Exit the program. */
333 }
334
335/* Before running the loop, check for UTF‐8 and whether CRLF is a valid newline
336sequence. First, find the options with which the regex was compiled and extract
337the UTF state. */
338
339(void)pcre2_pattern_info(re, PCRE2_INFO_ALLOPTIONS, &option_bits);
340utf8 = (option_bits & PCRE2_UTF) != 0;
341
342/* Now find the newline convention and see whether CRLF is a valid newline
343sequence. */
344
345(void)pcre2_pattern_info(re, PCRE2_INFO_NEWLINE, &newline);
346crlf_is_newline = newline == PCRE2_NEWLINE_ANY ||
347 newline == PCRE2_NEWLINE_CRLF ||
348 newline == PCRE2_NEWLINE_ANYCRLF;
349
350/* Loop for second and subsequent matches */
351
352for (;;)
353 {
354 uint32_t options = 0; /* Normally no options */
355 PCRE2_SIZE start_offset = ovector[1]; /* Start at end of previous match */
356
357 /* If the previous match was for an empty string, we are finished if we are
358 at the end of the subject. Otherwise, arrange to run another match at the
359 same point to see if a non‐empty match can be found. */
360
361 if (ovector[0] == ovector[1])
362 {
363 if (ovector[0] == subject_length) break;
364 options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
365 }
366
367 /* If the previous match was not an empty string, there is one tricky case to
368 consider. If a pattern contains \K within a lookbehind assertion at the
369 start, the end of the matched string can be at the offset where the match
370 started. Without special action, this leads to a loop that keeps on matching
371 the same substring. We must detect this case and arrange to move the start on
372 by one character. The pcre2_get_startchar() function returns the starting
373 offset that was passed to pcre2_match(). */
374
375 else
376 {
377 PCRE2_SIZE startchar = pcre2_get_startchar(match_data);
378 if (start_offset <= startchar)
379 {
380 if (startchar >= subject_length) break; /* Reached end of subject. */
381 start_offset = startchar + 1; /* Advance by one character. */
382 if (utf8) /* If UTF‐8, it may be more */
383 { /* than one code unit. */
384 for (; start_offset < subject_length; start_offset++)
385 if ((subject[start_offset] & 0xc0) != 0x80) break;
386 }
387 }
388 }
389
390 /* Run the next matching operation */
391
392 rc = pcre2_match(
393 re, /* the compiled pattern */
394 subject, /* the subject string */
395 subject_length, /* the length of the subject */
396 start_offset, /* starting offset in the subject */
397 options, /* options */
398 match_data, /* block for storing the result */
399 NULL); /* use default match context */
400
401 /* This time, a result of NOMATCH isn’t an error. If the value in "options"
402 is zero, it just means we have found all possible matches, so the loop ends.
403 Otherwise, it means we have failed to find a non‐empty‐string match at a
404 point where there was a previous empty‐string match. In this case, we do what
405 Perl does: advance the matching position by one character, and continue. We
406 do this by setting the "end of previous match" offset, because that is picked
407 up at the top of the loop as the point at which to start again.
408
409 There are two complications: (a) When CRLF is a valid newline sequence, and
410 the current position is just before it, advance by an extra byte. (b)
411 Otherwise we must ensure that we skip an entire UTF character if we are in
412 UTF mode. */
413
414 if (rc == PCRE2_ERROR_NOMATCH)
415 {
416 if (options == 0) break; /* All matches found */
417 ovector[1] = start_offset + 1; /* Advance one code unit */
418 if (crlf_is_newline && /* If CRLF is a newline & */
419 start_offset < subject_length ‐ 1 && /* we are at CRLF, */
420 subject[start_offset] == ’\r’ &&
421 subject[start_offset + 1] == ’\n’)
422 ovector[1] += 1; /* Advance by one more. */
423 else if (utf8) /* Otherwise, ensure we */
424 { /* advance a whole UTF‐8 */
425 while (ovector[1] < subject_length) /* character. */
426 {
427 if ((subject[ovector[1]] & 0xc0) != 0x80) break;
428 ovector[1] += 1;
429 }
430 }
431 continue; /* Go round the loop again */
432 }
433
434 /* Other matching errors are not recoverable. */
435
436 if (rc < 0)
437 {
438 printf("Matching error %d\n", rc);
439 pcre2_match_data_free(match_data);
440 pcre2_code_free(re);
441 return 1;
442 }
443
444 /* Match succeeded */
445
446 printf("\nMatch succeeded again at offset %d\n", (int)ovector[0]);
447
448 /* The match succeeded, but the output vector wasn’t big enough. This
449 should not happen. */
450
451 if (rc == 0)
452 printf("ovector was not big enough for all the captured substrings\n");
453
454 /* We must guard against patterns such as /(?=.\K)/ that use \K in an
455 assertion to set the start of a match later than its end. In this
456 demonstration program, we just detect this case and give up. */
457
458 if (ovector[0] > ovector[1])
459 {
460 printf("\\K was used in an assertion to set the match start after its end.\n"
461 "From end to start the match was: %.*s\n", (int)(ovector[0] ‐ ovector[1]),
462 (char *)(subject + ovector[1]));
463 printf("Run abandoned\n");
464 pcre2_match_data_free(match_data);
465 pcre2_code_free(re);
466 return 1;
467 }
468
469 /* As before, show substrings stored in the output vector by number, and then
470 also any named substrings. */
471
472 for (i = 0; i < rc; i++)
473 {
474 PCRE2_SPTR substring_start = subject + ovector[2*i];
475 size_t substring_length = ovector[2*i+1] ‐ ovector[2*i];
476 printf("%2d: %.*s\n", i, (int)substring_length, (char *)substring_start);
477 }
478
479 if (namecount == 0) printf("No named substrings\n"); else
480 {
481 PCRE2_SPTR tabptr = name_table;
482 printf("Named substrings\n");
483 for (i = 0; i < namecount; i++)
484 {
485 int n = (tabptr[0] << 8) | tabptr[1];
486 printf("(%d) %*s: %.*s\n", n, name_entry_size ‐ 3, tabptr + 2,
487 (int)(ovector[2*n+1] ‐ ovector[2*n]), subject + ovector[2*n]);
488 tabptr += name_entry_size;
489 }
490 }
491 } /* End of loop to find second and subsequent matches */
492
493printf("\n");
494pcre2_match_data_free(match_data);
495pcre2_code_free(re);
496return 0;
497}
498
499/* End of pcre2demo.c */
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528