• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
107
108size_t subject_length;
109pcre2_match_data *match_data;
110
111
112
113/**************************************************************************
114* First, sort out the command line. There is only one possible option at  *
115* the moment, "-g" to request repeated matching to find all occurrences,  *
116* like Perl's /g option. We set the variable find_all to a non-zero value *
117* if the -g option is present.                                            *
118**************************************************************************/
119
120find_all = 0;
121for (i = 1; i &lt; argc; i++)
122  {
123  if (strcmp(argv[i], "-g") == 0) find_all = 1;
124  else if (argv[i][0] == '-')
125    {
126    printf("Unrecognised option %s\n", argv[i]);
127    return 1;
128    }
129  else break;
130  }
131
132/* After the options, we require exactly two arguments, which are the pattern,
133and the subject string. */
134
135if (argc - i != 2)
136  {
137  printf("Exactly two arguments required: a regex and a subject string\n");
138  return 1;
139  }
140
141/* As pattern and subject are char arguments, they can be straightforwardly
142cast to PCRE2_SPTR as we are working in 8-bit code units. */
143
144pattern = (PCRE2_SPTR)argv[i];
145subject = (PCRE2_SPTR)argv[i+1];
146subject_length = strlen((char *)subject);
147
148
149/*************************************************************************
150* Now we are going to compile the regular expression pattern, and handle *
151* any errors that are detected.                                          *
152*************************************************************************/
153
154re = pcre2_compile(
155  pattern,               /* the pattern */
156  PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminated */
157  0,                     /* default options */
158  &amp;errornumber,          /* for error number */
159  &amp;erroroffset,          /* for error offset */
160  NULL);                 /* use default compile context */
161
162/* Compilation failed: print the error message and exit. */
163
164if (re == NULL)
165  {
166  PCRE2_UCHAR buffer[256];
167  pcre2_get_error_message(errornumber, buffer, sizeof(buffer));
168  printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset,
169    buffer);
170  return 1;
171  }
172
173
174/*************************************************************************
175* If the compilation succeeded, we call PCRE again, in order to do a     *
176* pattern match against the subject string. This does just ONE match. If *
177* further matching is needed, it will be done below. Before running the  *
178* match we must set up a match_data block for holding the result.        *
179*************************************************************************/
180
181/* Using this function ensures that the block is exactly the right size for
182the number of capturing parentheses in the pattern. */
183
184match_data = pcre2_match_data_create_from_pattern(re, NULL);
185
186rc = pcre2_match(
187  re,                   /* the compiled pattern */
188  subject,              /* the subject string */
189  subject_length,       /* the length of the subject */
190  0,                    /* start at offset 0 in the subject */
191  0,                    /* default options */
192  match_data,           /* block for storing the result */
193  NULL);                /* use default match context */
194
195/* Matching failed: handle error cases */
196
197if (rc &lt; 0)
198  {
199  switch(rc)
200    {
201    case PCRE2_ERROR_NOMATCH: printf("No match\n"); break;
202    /*
203    Handle other special cases if you like
204    */
205    default: printf("Matching error %d\n", rc); break;
206    }
207  pcre2_match_data_free(match_data);   /* Release memory used for the match */
208  pcre2_code_free(re);                 /* data and the compiled pattern. */
209  return 1;
210  }
211
212/* Match succeded. Get a pointer to the output vector, where string offsets are
213stored. */
214
215ovector = pcre2_get_ovector_pointer(match_data);
216printf("Match succeeded at offset %d\n", (int)ovector[0]);
217
218
219/*************************************************************************
220* We have found the first match within the subject string. If the output *
221* vector wasn't big enough, say so. Then output any substrings that were *
222* captured.                                                              *
223*************************************************************************/
224
225/* The output vector wasn't big enough. This should not happen, because we used
226pcre2_match_data_create_from_pattern() above. */
227
228if (rc == 0)
229  printf("ovector was not big enough for all the captured substrings\n");
230
231/* Show substrings stored in the output vector by number. Obviously, in a real
232application you might want to do things other than print them. */
233
234for (i = 0; i &lt; rc; i++)
235  {
236  PCRE2_SPTR substring_start = subject + ovector[2*i];
237  size_t substring_length = ovector[2*i+1] - ovector[2*i];
238  printf("%2d: %.*s\n", i, (int)substring_length, (char *)substring_start);
239  }
240
241
242/**************************************************************************
243* That concludes the basic part of this demonstration program. We have    *
244* compiled a pattern, and performed a single match. The code that follows *
245* shows first how to access named substrings, and then how to code for    *
246* repeated matches on the same subject.                                   *
247**************************************************************************/
248
249/* See if there are any named substrings, and if so, show them by name. First
250we have to extract the count of named parentheses from the pattern. */
251
252(void)pcre2_pattern_info(
253  re,                   /* the compiled pattern */
254  PCRE2_INFO_NAMECOUNT, /* get the number of named substrings */
255  &amp;namecount);          /* where to put the answer */
256
257if (namecount == 0) printf("No named substrings\n"); else
258  {
259  PCRE2_SPTR tabptr;
260  printf("Named substrings\n");
261
262  /* Before we can access the substrings, we must extract the table for
263  translating names to numbers, and the size of each entry in the table. */
264
265  (void)pcre2_pattern_info(
266    re,                       /* the compiled pattern */
267    PCRE2_INFO_NAMETABLE,     /* address of the table */
268    &amp;name_table);             /* where to put the answer */
269
270  (void)pcre2_pattern_info(
271    re,                       /* the compiled pattern */
272    PCRE2_INFO_NAMEENTRYSIZE, /* size of each entry in the table */
273    &amp;name_entry_size);        /* where to put the answer */
274
275  /* Now we can scan the table and, for each entry, print the number, the name,
276  and the substring itself. In the 8-bit library the number is held in two
277  bytes, most significant first. */
278
279  tabptr = name_table;
280  for (i = 0; i &lt; namecount; i++)
281    {
282    int n = (tabptr[0] &lt;&lt; 8) | tabptr[1];
283    printf("(%d) %*s: %.*s\n", n, name_entry_size - 3, tabptr + 2,
284      (int)(ovector[2*n+1] - ovector[2*n]), subject + ovector[2*n]);
285    tabptr += name_entry_size;
286    }
287  }
288
289
290/*************************************************************************
291* If the "-g" option was given on the command line, we want to continue  *
292* to search for additional matches in the subject string, in a similar   *
293* way to the /g option in Perl. This turns out to be trickier than you   *
294* might think because of the possibility of matching an empty string.    *
295* What happens is as follows:                                            *
296*                                                                        *
297* If the previous match was NOT for an empty string, we can just start   *
298* the next match at the end of the previous one.                         *
299*                                                                        *
300* If the previous match WAS for an empty string, we can't do that, as it *
301* would lead to an infinite loop. Instead, a call of pcre2_match() is    *
302* made with the PCRE2_NOTEMPTY_ATSTART and PCRE2_ANCHORED flags set. The *
303* first of these tells PCRE2 that an empty string at the start of the    *
304* subject is not a valid match; other possibilities must be tried. The   *
305* second flag restricts PCRE2 to one match attempt at the initial string *
306* position. If this match succeeds, an alternative to the empty string   *
307* match has been found, and we can print it and proceed round the loop,  *
308* advancing by the length of whatever was found. If this match does not  *
309* succeed, we still stay in the loop, advancing by just one character.   *
310* In UTF-8 mode, which can be set by (*UTF) in the pattern, this may be  *
311* more than one byte.                                                    *
312*                                                                        *
313* However, there is a complication concerned with newlines. When the     *
314* newline convention is such that CRLF is a valid newline, we must       *
315* advance by two characters rather than one. The newline convention can  *
316* be set in the regex by (*CR), etc.; if not, we must find the default.  *
317*************************************************************************/
318
319if (!find_all)     /* Check for -g */
320  {
321  pcre2_match_data_free(match_data);  /* Release the memory that was used */
322  pcre2_code_free(re);                /* for the match data and the pattern. */
323  return 0;                           /* Exit the program. */
324  }
325
326/* Before running the loop, check for UTF-8 and whether CRLF is a valid newline
327sequence. First, find the options with which the regex was compiled and extract
328the UTF state. */
329
330(void)pcre2_pattern_info(re, PCRE2_INFO_ALLOPTIONS, &amp;option_bits);
331utf8 = (option_bits &amp; PCRE2_UTF) != 0;
332
333/* Now find the newline convention and see whether CRLF is a valid newline
334sequence. */
335
336(void)pcre2_pattern_info(re, PCRE2_INFO_NEWLINE, &amp;newline);
337crlf_is_newline = newline == PCRE2_NEWLINE_ANY ||
338                  newline == PCRE2_NEWLINE_CRLF ||
339                  newline == PCRE2_NEWLINE_ANYCRLF;
340
341/* Loop for second and subsequent matches */
342
343for (;;)
344  {
345  uint32_t options = 0;                   /* Normally no options */
346  PCRE2_SIZE start_offset = ovector[1];   /* Start at end of previous match */
347
348  /* If the previous match was for an empty string, we are finished if we are
349  at the end of the subject. Otherwise, arrange to run another match at the
350  same point to see if a non-empty match can be found. */
351
352  if (ovector[0] == ovector[1])
353    {
354    if (ovector[0] == subject_length) break;
355    options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
356    }
357
358  /* Run the next matching operation */
359
360  rc = pcre2_match(
361    re,                   /* the compiled pattern */
362    subject,              /* the subject string */
363    subject_length,       /* the length of the subject */
364    start_offset,         /* starting offset in the subject */
365    options,              /* options */
366    match_data,           /* block for storing the result */
367    NULL);                /* use default match context */
368
369  /* This time, a result of NOMATCH isn't an error. If the value in "options"
370  is zero, it just means we have found all possible matches, so the loop ends.
371  Otherwise, it means we have failed to find a non-empty-string match at a
372  point where there was a previous empty-string match. In this case, we do what
373  Perl does: advance the matching position by one character, and continue. We
374  do this by setting the "end of previous match" offset, because that is picked
375  up at the top of the loop as the point at which to start again.
376
377  There are two complications: (a) When CRLF is a valid newline sequence, and
378  the current position is just before it, advance by an extra byte. (b)
379  Otherwise we must ensure that we skip an entire UTF character if we are in
380  UTF mode. */
381
382  if (rc == PCRE2_ERROR_NOMATCH)
383    {
384    if (options == 0) break;                    /* All matches found */
385    ovector[1] = start_offset + 1;              /* Advance one code unit */
386    if (crlf_is_newline &amp;&amp;                      /* If CRLF is a newline &amp; */
387        start_offset &lt; subject_length - 1 &amp;&amp;    /* we are at CRLF, */
388        subject[start_offset] == '\r' &amp;&amp;
389        subject[start_offset + 1] == '\n')
390      ovector[1] += 1;                          /* Advance by one more. */
391    else if (utf8)                              /* Otherwise, ensure we */
392      {                                         /* advance a whole UTF-8 */
393      while (ovector[1] &lt; subject_length)       /* character. */
394        {
395        if ((subject[ovector[1]] &amp; 0xc0) != 0x80) break;
396        ovector[1] += 1;
397        }
398      }
399    continue;    /* Go round the loop again */
400    }
401
402  /* Other matching errors are not recoverable. */
403
404  if (rc &lt; 0)
405    {
406    printf("Matching error %d\n", rc);
407    pcre2_match_data_free(match_data);
408    pcre2_code_free(re);
409    return 1;
410    }
411
412  /* Match succeded */
413
414  printf("\nMatch succeeded again at offset %d\n", (int)ovector[0]);
415
416  /* The match succeeded, but the output vector wasn't big enough. This
417  should not happen. */
418
419  if (rc == 0)
420    printf("ovector was not big enough for all the captured substrings\n");
421
422  /* As before, show substrings stored in the output vector by number, and then
423  also any named substrings. */
424
425  for (i = 0; i &lt; rc; i++)
426    {
427    PCRE2_SPTR substring_start = subject + ovector[2*i];
428    size_t substring_length = ovector[2*i+1] - ovector[2*i];
429    printf("%2d: %.*s\n", i, (int)substring_length, (char *)substring_start);
430    }
431
432  if (namecount == 0) printf("No named substrings\n"); else
433    {
434    PCRE2_SPTR tabptr = name_table;
435    printf("Named substrings\n");
436    for (i = 0; i &lt; namecount; i++)
437      {
438      int n = (tabptr[0] &lt;&lt; 8) | tabptr[1];
439      printf("(%d) %*s: %.*s\n", n, name_entry_size - 3, tabptr + 2,
440        (int)(ovector[2*n+1] - ovector[2*n]), subject + ovector[2*n]);
441      tabptr += name_entry_size;
442      }
443    }
444  }      /* End of loop to find second and subsequent matches */
445
446printf("\n");
447pcre2_match_data_free(match_data);
448pcre2_code_free(re);
449return 0;
450}
451
452/* End of pcre2demo.c */
453<p>
454Return to the <a href="index.html">PCRE2 index page</a>.
455</p>
456