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.  A
12       listing  of this program is given in the pcredemo documentation. If you
13       do not have a copy of the PCRE distribution, you can save this  listing
14       to re-create pcredemo.c.
15
16       The program compiles the regular expression that is its first argument,
17       and matches it against the subject string in its  second  argument.  No
18       PCRE  options are set, and default character tables are used. If match‐
19       ing succeeds, the program outputs  the  portion  of  the  subject  that
20       matched, together with the contents of any captured substrings.
21
22       If the -g option is given on the command line, the program then goes on
23       to check for further matches of the same regular expression in the same
24       subject  string. The logic is a little bit tricky because of the possi‐
25       bility of matching an empty string. Comments in the code  explain  what
26       is going on.
27
28       If  PCRE  is  installed in the standard include and library directories
29       for your operating system, you should be able to compile the demonstra‐
30       tion program using this command:
31
32         gcc -o pcredemo pcredemo.c -lpcre
33
34       If  PCRE is installed elsewhere, you may need to add additional options
35       to the command line. For example, on a Unix-like system that  has  PCRE
36       installed  in  /usr/local,  you  can  compile the demonstration program
37       using a command like this:
38
39         gcc -o pcredemo -I/usr/local/include pcredemo.c \
40             -L/usr/local/lib -lpcre
41
42       In a Windows environment, if you want to statically  link  the  program
43       against a non-dll pcre.a file, you must uncomment the line that defines
44       PCRE_STATIC before including pcre.h, because  otherwise  the  pcre_mal‐
45       loc()   and   pcre_free()   exported   functions   will   be   declared
46       __declspec(dllimport), with unwanted results.
47
48       Once you have compiled and linked the demonstration  program,  you  can
49       run simple tests like this:
50
51         ./pcredemo 'cat|dog' 'the cat sat on the mat'
52         ./pcredemo -g 'cat|dog' 'the dog sat on the cat'
53
54       Note  that  there  is  a  much  more comprehensive test program, called
55       pcretest, which supports  many  more  facilities  for  testing  regular
56       expressions and the PCRE library. The pcredemo program is provided as a
57       simple coding example.
58
59       When you try to run pcredemo when PCRE is not installed in the standard
60       library  directory,  you  may  get an error like this on some operating
61       systems (e.g. Solaris):
62
63         ld.so.1: a.out: fatal: libpcre.so.0: open failed:  No  such  file  or
64       directory
65
66       This  is  caused  by the way shared library support works on those sys‐
67       tems. You need to add
68
69         -R/usr/local/lib
70
71       (for example) to the compile command to get round this problem.
72

AUTHOR

74
75       Philip Hazel
76       University Computing Service
77       Cambridge CB2 3QH, England.
78

REVISION

80
81       Last updated: 26 May 2010
82       Copyright (c) 1997-2010 University of Cambridge.
83
84
85
86                                                                 PCRESAMPLE(3)
Impressum