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       If PCRE is installed in the standard include  and  library  directories
26       for  your  system, you should be able to compile the demonstration pro‐
27       gram using this command:
28
29         gcc -o pcredemo pcredemo.c -lpcre
30
31       If PCRE is installed elsewhere, you may need to add additional  options
32       to  the  command line. For example, on a Unix-like system that has PCRE
33       installed in /usr/local, you  can  compile  the  demonstration  program
34       using a command like this:
35
36         gcc -o pcredemo -I/usr/local/include pcredemo.c \
37             -L/usr/local/lib -lpcre
38
39       Once  you  have  compiled the demonstration program, you can run simple
40       tests like this:
41
42         ./pcredemo 'cat|dog' 'the cat sat on the mat'
43         ./pcredemo -g 'cat|dog' 'the dog sat on the cat'
44
45       Note that there is a  much  more  comprehensive  test  program,  called
46       pcretest,  which  supports  many  more  facilities  for testing regular
47       expressions and the PCRE library. The pcredemo program is provided as a
48       simple coding example.
49
50       On some operating systems (e.g. Solaris), when PCRE is not installed in
51       the standard library directory, you may get an error like this when you
52       try to run pcredemo:
53
54         ld.so.1:  a.out:  fatal:  libpcre.so.0:  open failed: No such file or
55       directory
56
57       This is caused by the way shared library support works  on  those  sys‐
58       tems. You need to add
59
60         -R/usr/local/lib
61
62       (for example) to the compile command to get round this problem.
63

AUTHOR

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

REVISION

71
72       Last updated: 23 January 2008
73       Copyright (c) 1997-2008 University of Cambridge.
74
75
76
77                                                                 PCRESAMPLE(3)
Impressum