blob: c418f7e57261925ef278d4be6bcdd0d9bc5c6f4c [file] [log] [blame]
Tristan Matthews04616462013-11-14 16:09:34 -05001.TH PCRESAMPLE 3
2.SH NAME
3PCRE - Perl-compatible regular expressions
4.SH "PCRE SAMPLE PROGRAM"
5.rs
6.sp
7A simple, complete demonstration program, to get you started with using PCRE,
8is supplied in the file \fIpcredemo.c\fP in the PCRE distribution. A listing of
9this program is given in the
10.\" HREF
11\fBpcredemo\fP
12.\"
13documentation. If you do not have a copy of the PCRE distribution, you can save
14this listing to re-create \fIpcredemo.c\fP.
15.P
16The program compiles the regular expression that is its first argument, and
17matches it against the subject string in its second argument. No PCRE options
18are set, and default character tables are used. If matching succeeds, the
19program outputs the portion of the subject that matched, together with the
20contents of any captured substrings.
21.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
27If PCRE is installed in the standard include and library directories for your
28operating system, you should be able to compile the demonstration program using
29this command:
30.sp
31 gcc -o pcredemo pcredemo.c -lpcre
32.sp
33If PCRE is installed elsewhere, you may need to add additional options to the
34command line. For example, on a Unix-like system that has PCRE installed in
35\fI/usr/local\fP, you can compile the demonstration program using a command
36like this:
37.sp
38.\" JOINSH
39 gcc -o pcredemo -I/usr/local/include pcredemo.c \e
40 -L/usr/local/lib -lpcre
41.sp
42In a Windows environment, if you want to statically link the program against a
43non-dll \fBpcre.a\fP file, you must uncomment the line that defines PCRE_STATIC
44before including \fBpcre.h\fP, because otherwise the \fBpcre_malloc()\fP and
45\fBpcre_free()\fP exported functions will be declared
46\fB__declspec(dllimport)\fP, with unwanted results.
47.P
48Once you have compiled and linked the demonstration program, you can run simple
49tests like this:
50.sp
51 ./pcredemo 'cat|dog' 'the cat sat on the mat'
52 ./pcredemo -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\fBpcretest\fP,
57.\"
58which supports many more facilities for testing regular expressions and the
59PCRE library. The
60.\" HREF
61\fBpcredemo\fP
62.\"
63program is provided as a simple coding example.
64.P
65If you try to run
66.\" HREF
67\fBpcredemo\fP
68.\"
69when PCRE is not installed in the standard library directory, you may get an
70error like this on some operating systems (e.g. Solaris):
71.sp
72 ld.so.1: a.out: fatal: libpcre.so.0: open failed: No such file or directory
73.sp
74This is caused by the way shared library support works on those systems. You
75need to add
76.sp
77 -R/usr/local/lib
78.sp
79(for example) to the compile command to get round this problem.
80.
81.
82.SH AUTHOR
83.rs
84.sp
85.nf
86Philip Hazel
87University Computing Service
88Cambridge CB2 3QH, England.
89.fi
90.
91.
92.SH REVISION
93.rs
94.sp
95.nf
96Last updated: 17 November 2010
97Copyright (c) 1997-2010 University of Cambridge.
98.fi