1 |
.TH PCRESAMPLE 3
|
2 |
.SH NAME
|
3 |
PCRE - Perl-compatible regular expressions
|
4 |
.SH "PCRE SAMPLE PROGRAM"
|
5 |
.rs
|
6 |
.sp
|
7 |
A simple, complete demonstration program, to get you started with using PCRE,
|
8 |
is supplied in the file \fIpcredemo.c\fP in the PCRE distribution. A listing of
|
9 |
this program is given in the
|
10 |
.\" HREF
|
11 |
\fBpcredemo\fP
|
12 |
.\"
|
13 |
documentation. If you do not have a copy of the PCRE distribution, you can save
|
14 |
this listing to re-create \fIpcredemo.c\fP.
|
15 |
.P
|
16 |
The program compiles the regular expression that is its first argument, and
|
17 |
matches it against the subject string in its second argument. No PCRE options
|
18 |
are set, and default character tables are used. If matching succeeds, the
|
19 |
program outputs the portion of the subject that matched, together with the
|
20 |
contents of any captured substrings.
|
21 |
.P
|
22 |
If the -g option is given on the command line, the program then goes on to
|
23 |
check for further matches of the same regular expression in the same subject
|
24 |
string. The logic is a little bit tricky because of the possibility of matching
|
25 |
an empty string. Comments in the code explain what is going on.
|
26 |
.P
|
27 |
If PCRE is installed in the standard include and library directories for your
|
28 |
system, you should be able to compile the demonstration program using this
|
29 |
command:
|
30 |
.sp
|
31 |
gcc -o pcredemo pcredemo.c -lpcre
|
32 |
.sp
|
33 |
If PCRE is installed elsewhere, you may need to add additional options to the
|
34 |
command 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
|
36 |
like this:
|
37 |
.sp
|
38 |
.\" JOINSH
|
39 |
gcc -o pcredemo -I/usr/local/include pcredemo.c \e
|
40 |
-L/usr/local/lib -lpcre
|
41 |
.sp
|
42 |
Once you have compiled the demonstration program, you can run simple tests like
|
43 |
this:
|
44 |
.sp
|
45 |
./pcredemo 'cat|dog' 'the cat sat on the mat'
|
46 |
./pcredemo -g 'cat|dog' 'the dog sat on the cat'
|
47 |
.sp
|
48 |
Note that there is a much more comprehensive test program, called
|
49 |
.\" HREF
|
50 |
\fBpcretest\fP,
|
51 |
.\"
|
52 |
which supports many more facilities for testing regular expressions and the
|
53 |
PCRE library. The
|
54 |
.\" HREF
|
55 |
\fBpcredemo\fP
|
56 |
.\"
|
57 |
program is provided as a simple coding example.
|
58 |
.P
|
59 |
When you try to run
|
60 |
.\" HREF
|
61 |
\fBpcredemo\fP
|
62 |
.\"
|
63 |
when PCRE is not installed in the standard library directory, you may get an
|
64 |
error like this on some operating systems (e.g. Solaris):
|
65 |
.sp
|
66 |
ld.so.1: a.out: fatal: libpcre.so.0: open failed: No such file or directory
|
67 |
.sp
|
68 |
This is caused by the way shared library support works on those systems. You
|
69 |
need to add
|
70 |
.sp
|
71 |
-R/usr/local/lib
|
72 |
.sp
|
73 |
(for example) to the compile command to get round this problem.
|
74 |
.
|
75 |
.
|
76 |
.SH AUTHOR
|
77 |
.rs
|
78 |
.sp
|
79 |
.nf
|
80 |
Philip Hazel
|
81 |
University Computing Service
|
82 |
Cambridge CB2 3QH, England.
|
83 |
.fi
|
84 |
.
|
85 |
.
|
86 |
.SH REVISION
|
87 |
.rs
|
88 |
.sp
|
89 |
.nf
|
90 |
Last updated: 01 September 2009
|
91 |
Copyright (c) 1997-2009 University of Cambridge.
|
92 |
.fi
|