1PCRE2SAMPLE(3) Library Functions Manual PCRE2SAMPLE(3)
2
3
4
6 PCRE2 - Perl-compatible regular expressions (revised API)
7
9
10 A simple, complete demonstration program to get you started with using
11 PCRE2 is supplied in the file pcre2demo.c in the src directory in the
12 PCRE2 distribution. A listing of this program is given in the pcre2demo
13 documentation. If you do not have a copy of the PCRE2 distribution, you
14 can save this listing to re-create the contents of pcre2demo.c.
15
16 The demonstration program compiles the regular expression that is its
17 first argument, and matches it against the subject string in its second
18 argument. No PCRE2 options are set, and default character tables are
19 used. If matching succeeds, the program outputs the portion of the sub‐
20 ject that matched, together with the contents of any captured sub‐
21 strings.
22
23 If the -g option is given on the command line, the program then goes on
24 to check for further matches of the same regular expression in the same
25 subject string. The logic is a little bit tricky because of the possi‐
26 bility of matching an empty string. Comments in the code explain what
27 is going on.
28
29 The code in pcre2demo.c is an 8-bit program that uses the PCRE2 8-bit
30 library. It handles strings and characters that are stored in 8-bit
31 code units. By default, one character corresponds to one code unit,
32 but if the pattern starts with "(*UTF)", both it and the subject are
33 treated as UTF-8 strings, where characters may occupy multiple code
34 units.
35
36 If PCRE2 is installed in the standard include and library directories
37 for your operating system, you should be able to compile the demonstra‐
38 tion program using a command like this:
39
40 cc -o pcre2demo pcre2demo.c -lpcre2-8
41
42 If PCRE2 is installed elsewhere, you may need to add additional options
43 to the command line. For example, on a Unix-like system that has PCRE2
44 installed in /usr/local, you can compile the demonstration program
45 using a command like this:
46
47 cc -o pcre2demo -I/usr/local/include pcre2demo.c \
48 -L/usr/local/lib -lpcre2-8
49
50 Once you have built the demonstration program, you can run simple tests
51 like this:
52
53 ./pcre2demo 'cat|dog' 'the cat sat on the mat'
54 ./pcre2demo -g 'cat|dog' 'the dog sat on the cat'
55
56 Note that there is a much more comprehensive test program, called
57 pcre2test, which supports many more facilities for testing regular
58 expressions using all three PCRE2 libraries (8-bit, 16-bit, and 32-bit,
59 though not all three need be installed). The pcre2demo program is pro‐
60 vided as a relatively simple coding example.
61
62 If you try to run pcre2demo when PCRE2 is not installed in the standard
63 library directory, you may get an error like this on some operating
64 systems (e.g. Solaris):
65
66 ld.so.1: pcre2demo: fatal: libpcre2-8.so.0: open failed: No such file
67 or directory
68
69 This is caused by the way shared library support works on those sys‐
70 tems. You need to add
71
72 -R/usr/local/lib
73
74 (for example) to the compile command to get round this problem.
75
77
78 Philip Hazel
79 University Computing Service
80 Cambridge, England.
81
83
84 Last updated: 02 February 2016
85 Copyright (c) 1997-2016 University of Cambridge.
86
87
88
89PCRE2 10.22 02 February 2016 PCRE2SAMPLE(3)