1PCRESAMPLE(3)              Library Functions Manual              PCRESAMPLE(3)
2
3
4

NAME

6       PCRE - Perl-compatible regular expressions
7

PCRE SAMPLE PROGRAM

9
10       A simple, complete demonstration program, to get you started with using
11       PCRE, is supplied in the file pcredemo.c in the PCRE distribution.
12
13       The program compiles the regular expression that is its first argument,
14       and  matches  it  against the subject string in its second argument. No
15       PCRE options are set, and default character tables are used. If  match‐
16       ing  succeeds,  the  program  outputs  the  portion of the subject that
17       matched, together with the contents of any captured substrings.
18
19       If the -g option is given on the command line, the program then goes on
20       to check for further matches of the same regular expression in the same
21       subject string. The logic is a little bit tricky because of the  possi‐
22       bility  of  matching an empty string. Comments in the code explain what
23       is going on.
24
25       The demonstration program is automatically built if you use  "./config‐
26       ure;make"  to  build PCRE. Otherwise, if PCRE is installed in the stan‐
27       dard include and library directories for your  system,  you  should  be
28       able to compile the demonstration program using this command:
29
30         gcc -o pcredemo pcredemo.c -lpcre
31
32       If  PCRE is installed elsewhere, you may need to add additional options
33       to the command line. For example, on a Unix-like system that  has  PCRE
34       installed  in  /usr/local,  you  can  compile the demonstration program
35       using a command like this:
36
37         gcc -o pcredemo -I/usr/local/include pcredemo.c \
38             -L/usr/local/lib -lpcre
39
40       Once you have compiled the demonstration program, you  can  run  simple
41       tests like this:
42
43         ./pcredemo 'cat|dog' 'the cat sat on the mat'
44         ./pcredemo -g 'cat|dog' 'the dog sat on the cat'
45
46       Note  that  there  is  a  much  more comprehensive test program, called
47       pcretest, which supports  many  more  facilities  for  testing  regular
48       expressions and the PCRE library. The pcredemo program is provided as a
49       simple coding example.
50
51       On some operating systems (e.g. Solaris), when PCRE is not installed in
52       the standard library directory, you may get an error like this when you
53       try to run pcredemo:
54
55         ld.so.1: a.out: fatal: libpcre.so.0: open failed:  No  such  file  or
56       directory
57
58       This  is  caused  by the way shared library support works on those sys‐
59       tems. You need to add
60
61         -R/usr/local/lib
62
63       (for example) to the compile command to get round this problem.
64

AUTHOR

66
67       Philip Hazel
68       University Computing Service
69       Cambridge CB2 3QH, England.
70

REVISION

72
73       Last updated: 13 June 2007
74       Copyright (c) 1997-2007 University of Cambridge.
75
76
77
78                                                                 PCRESAMPLE(3)
Impressum