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

AUTHOR

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

REVISION

81
82       Last updated: 10 January 2012
83       Copyright (c) 1997-2012 University of Cambridge.
84
85
86
87PCRE 8.30                       10 January 2012                  PCRESAMPLE(3)
Impressum