blob: 661e3927417c89cf72230de3b611b9e2f5bdb6f3 [file] [log] [blame]
Janis Danisevskis8b979b22016-08-15 16:09:16 +01001.TH PCRE2SAMPLE 3 "02 February 2016" "PCRE2 10.22"
Janis Danisevskis53e448c2016-03-31 13:35:25 +01002.SH NAME
3PCRE2 - Perl-compatible regular expressions (revised API)
4.SH "PCRE2 SAMPLE PROGRAM"
5.rs
6.sp
7A simple, complete demonstration program to get you started with using PCRE2 is
8supplied in the file \fIpcre2demo.c\fP in the \fBsrc\fP directory in the PCRE2
9distribution. A listing of this program is given in the
10.\" HREF
11\fBpcre2demo\fP
12.\"
13documentation. If you do not have a copy of the PCRE2 distribution, you can
14save this listing to re-create the contents of \fIpcre2demo.c\fP.
15.P
Janis Danisevskis8b979b22016-08-15 16:09:16 +010016The demonstration program compiles the regular expression that is its
17first argument, and matches it against the subject string in its second
18argument. No PCRE2 options are set, and default character tables are used. If
19matching succeeds, the program outputs the portion of the subject that matched,
20together with the contents of any captured substrings.
Janis Danisevskis53e448c2016-03-31 13:35:25 +010021.P
22If the -g option is given on the command line, the program then goes on to
23check for further matches of the same regular expression in the same subject
24string. The logic is a little bit tricky because of the possibility of matching
25an empty string. Comments in the code explain what is going on.
26.P
Janis Danisevskis8b979b22016-08-15 16:09:16 +010027The code in \fBpcre2demo.c\fP is an 8-bit program that uses the PCRE2 8-bit
28library. It handles strings and characters that are stored in 8-bit code units.
29By default, one character corresponds to one code unit, but if the pattern
30starts with "(*UTF)", both it and the subject are treated as UTF-8 strings,
31where characters may occupy multiple code units.
32.P
Janis Danisevskis53e448c2016-03-31 13:35:25 +010033If PCRE2 is installed in the standard include and library directories for your
34operating system, you should be able to compile the demonstration program using
Janis Danisevskis8b979b22016-08-15 16:09:16 +010035a command like this:
Janis Danisevskis53e448c2016-03-31 13:35:25 +010036.sp
Janis Danisevskis8b979b22016-08-15 16:09:16 +010037 cc -o pcre2demo pcre2demo.c -lpcre2-8
Janis Danisevskis53e448c2016-03-31 13:35:25 +010038.sp
39If PCRE2 is installed elsewhere, you may need to add additional options to the
40command line. For example, on a Unix-like system that has PCRE2 installed in
41\fI/usr/local\fP, you can compile the demonstration program using a command
42like this:
43.sp
44.\" JOINSH
Janis Danisevskis8b979b22016-08-15 16:09:16 +010045 cc -o pcre2demo -I/usr/local/include pcre2demo.c \e
46 -L/usr/local/lib -lpcre2-8
Janis Danisevskis53e448c2016-03-31 13:35:25 +010047.sp
Janis Danisevskis8b979b22016-08-15 16:09:16 +010048Once you have built the demonstration program, you can run simple tests like
49this:
Janis Danisevskis53e448c2016-03-31 13:35:25 +010050.sp
51 ./pcre2demo 'cat|dog' 'the cat sat on the mat'
52 ./pcre2demo -g 'cat|dog' 'the dog sat on the cat'
53.sp
54Note that there is a much more comprehensive test program, called
55.\" HREF
56\fBpcre2test\fP,
57.\"
Janis Danisevskis8b979b22016-08-15 16:09:16 +010058which supports many more facilities for testing regular expressions using all
59three PCRE2 libraries (8-bit, 16-bit, and 32-bit, though not all three need be
60installed). The
Janis Danisevskis53e448c2016-03-31 13:35:25 +010061.\" HREF
62\fBpcre2demo\fP
63.\"
Janis Danisevskis8b979b22016-08-15 16:09:16 +010064program is provided as a relatively simple coding example.
Janis Danisevskis53e448c2016-03-31 13:35:25 +010065.P
66If you try to run
67.\" HREF
68\fBpcre2demo\fP
69.\"
70when PCRE2 is not installed in the standard library directory, you may get an
71error like this on some operating systems (e.g. Solaris):
72.sp
Janis Danisevskis8b979b22016-08-15 16:09:16 +010073 ld.so.1: pcre2demo: fatal: libpcre2-8.so.0: open failed: No such file or directory
Janis Danisevskis53e448c2016-03-31 13:35:25 +010074.sp
75This is caused by the way shared library support works on those systems. You
76need to add
77.sp
78 -R/usr/local/lib
79.sp
80(for example) to the compile command to get round this problem.
81.
82.
83.SH AUTHOR
84.rs
85.sp
86.nf
87Philip Hazel
88University Computing Service
89Cambridge, England.
90.fi
91.
92.
93.SH REVISION
94.rs
95.sp
96.nf
Janis Danisevskis8b979b22016-08-15 16:09:16 +010097Last updated: 02 February 2016
98Copyright (c) 1997-2016 University of Cambridge.
Janis Danisevskis53e448c2016-03-31 13:35:25 +010099.fi