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