1 |
README file for PCRE (Perl-compatible regular expressions)
|
2 |
----------------------------------------------------------
|
3 |
|
4 |
The distribution should contain the following files:
|
5 |
|
6 |
ChangeLog log of changes to the code
|
7 |
Makefile for building PCRE
|
8 |
Performance notes on performance
|
9 |
README this file
|
10 |
Tech.Notes notes on the encoding
|
11 |
pcre.3 man page for the functions
|
12 |
pcreposix.3 man page for the POSIX wrapper API
|
13 |
maketables.c auxiliary program for building chartables.c
|
14 |
study.c ) source of
|
15 |
pcre.c ) the functions
|
16 |
pcreposix.c )
|
17 |
pcre.h header for the external API
|
18 |
pcreposix.h header for the external POSIX wrapper API
|
19 |
internal.h header for internal use
|
20 |
pcretest.c test program
|
21 |
pgrep.1 man page for pgrep
|
22 |
pgrep.c source of a grep utility that uses PCRE
|
23 |
perltest Perl test program
|
24 |
testinput test data, compatible with Perl
|
25 |
testinput2 test data for error messages and non-Perl things
|
26 |
testoutput test results corresponding to testinput
|
27 |
testoutput2 test results corresponding to testinput2
|
28 |
|
29 |
To build PCRE, edit Makefile for your system (it is a fairly simple make file)
|
30 |
and then run it. It builds a two libraries called libpcre.a and libpcreposix.a,
|
31 |
a test program called pcretest, and the pgrep command.
|
32 |
|
33 |
To test PCRE, run pcretest on the file testinput, and compare the output with
|
34 |
the contents of testoutput. There should be no differences. For example:
|
35 |
|
36 |
pcretest testinput some.file
|
37 |
diff some.file testoutput
|
38 |
|
39 |
Do the same with testinput2, comparing the output with testoutput2, but this
|
40 |
time using the -i flag for pcretest, i.e.
|
41 |
|
42 |
pcretest -i testinput2 some.file
|
43 |
diff some.file testoutput2
|
44 |
|
45 |
The make target "runtest" runs both these tests, using the file "testtry" to
|
46 |
store the intermediate output, deleting it at the end if all goes well.
|
47 |
|
48 |
There are two sets of tests because the first set can also be fed directly into
|
49 |
the perltest program to check that Perl gives the same results. The second set
|
50 |
of tests check pcre_info(), pcre_study(), error detection and run-time flags
|
51 |
that are specific to PCRE, as well as the POSIX wrapper API.
|
52 |
|
53 |
To install PCRE, copy libpcre.a to any suitable library directory (e.g.
|
54 |
/usr/local/lib), pcre.h to any suitable include directory (e.g.
|
55 |
/usr/local/include), and pcre.3 to any suitable man directory (e.g.
|
56 |
/usr/local/man/man3).
|
57 |
|
58 |
To install the pgrep command, copy it to any suitable binary directory, (e.g.
|
59 |
/usr/local/bin) and pgrep.1 to any suitable man directory (e.g.
|
60 |
/usr/local/man/man1).
|
61 |
|
62 |
PCRE has its own native API, but a set of "wrapper" functions that are based on
|
63 |
the POSIX API are also supplied in the library libpcreposix.a. Note that this
|
64 |
just provides a POSIX calling interface to PCRE: the regular expressions
|
65 |
themselves still follow Perl syntax and semantics. The header file
|
66 |
for the POSIX-style functions is called pcreposix.h. The official POSIX name is
|
67 |
regex.h, but I didn't want to risk possible problems with existing files of
|
68 |
that name by distributing it that way. To use it with an existing program that
|
69 |
uses the POSIX API it will have to be renamed or pointed at by a link.
|
70 |
|
71 |
|
72 |
Character tables
|
73 |
----------------
|
74 |
|
75 |
PCRE uses four tables for manipulating and identifying characters. These are
|
76 |
compiled from a source file called chartables.c. This is not supplied in
|
77 |
the distribution, but is built by the program maketables (compiled from
|
78 |
maketables.c), which uses the ANSI C character handling functions such as
|
79 |
isalnum(), isalpha(), isupper(), islower(), etc. to build the table sources.
|
80 |
This means that the default C locale set in your system may affect the contents
|
81 |
of the tables. You can change the tables by editing chartables.c and then
|
82 |
re-building PCRE. If you do this, you should probably also edit Makefile to
|
83 |
ensure that the file doesn't ever get re-generated.
|
84 |
|
85 |
The first two tables pcre_lcc[] and pcre_fcc[] provide lower casing and a
|
86 |
case flipping functions, respectively. The pcre_cbits[] table consists of four
|
87 |
32-byte bit maps which identify digits, letters, "word" characters, and white
|
88 |
space, respectively. These are used when building 32-byte bit maps that
|
89 |
represent character classes.
|
90 |
|
91 |
The pcre_ctypes[] table has bits indicating various character types, as
|
92 |
follows:
|
93 |
|
94 |
1 white space character
|
95 |
2 letter
|
96 |
4 decimal digit
|
97 |
8 hexadecimal digit
|
98 |
16 alphanumeric or '_'
|
99 |
128 regular expression metacharacter or binary zero
|
100 |
|
101 |
You should not alter the set of characters that contain the 128 bit, as that
|
102 |
will cause PCRE to malfunction.
|
103 |
|
104 |
|
105 |
The pcretest program
|
106 |
--------------------
|
107 |
|
108 |
This program is intended for testing PCRE, but it can also be used for
|
109 |
experimenting with regular expressions.
|
110 |
|
111 |
If it is given two filename arguments, it reads from the first and writes to
|
112 |
the second. If it is given only one filename argument, it reads from that file
|
113 |
and writes to stdout. Otherwise, it reads from stdin and writes to stdout, and
|
114 |
prompts for each line of input.
|
115 |
|
116 |
The program handles any number of sets of input on a single input file. Each
|
117 |
set starts with a regular expression, and continues with any number of data
|
118 |
lines to be matched against the pattern. An empty line signals the end of the
|
119 |
set. The regular expressions are given enclosed in any non-alphameric
|
120 |
delimiters, for example
|
121 |
|
122 |
/(a|bc)x+yz/
|
123 |
|
124 |
and may be followed by i, m, s, or x to set the PCRE_CASELESS, PCRE_MULTILINE,
|
125 |
PCRE_DOTALL, or PCRE_EXTENDED options, respectively. These options have the
|
126 |
same effect as they do in Perl.
|
127 |
|
128 |
There are also some upper case options that do not match Perl options: /A, /E,
|
129 |
and /X set PCRE_ANCHORED, PCRE_DOLLAR_ENDONLY, and PCRE_EXTRA respectively.
|
130 |
The /D option is a PCRE debugging feature. It causes the internal form of
|
131 |
compiled regular expressions to be output after compilation. The /S option
|
132 |
causes pcre_study() to be called after the expression has been compiled, and
|
133 |
the results used when the expression is matched. If /I is present as well as
|
134 |
/S, then pcre_study() is called with the PCRE_CASELESS option.
|
135 |
|
136 |
Finally, the /P option causes pcretest to call PCRE via the POSIX wrapper API
|
137 |
rather than its native API. When this is done, all other options except /i and
|
138 |
/m are ignored. REG_ICASE is set if /i is present, and REG_NEWLINE is set if /m
|
139 |
is present. The wrapper functions force PCRE_DOLLAR_ENDONLY always, and
|
140 |
PCRE_DOTALL unless REG_NEWLINE is set.
|
141 |
|
142 |
A regular expression can extend over several lines of input; the newlines are
|
143 |
included in it. See the testinput file for many examples.
|
144 |
|
145 |
Before each data line is passed to pcre_exec(), leading and trailing whitespace
|
146 |
is removed, and it is then scanned for \ escapes. The following are recognized:
|
147 |
|
148 |
\a alarm (= BEL)
|
149 |
\b backspace
|
150 |
\e escape
|
151 |
\f formfeed
|
152 |
\n newline
|
153 |
\r carriage return
|
154 |
\t tab
|
155 |
\v vertical tab
|
156 |
\nnn octal character (up to 3 octal digits)
|
157 |
\xhh hexadecimal character (up to 2 hex digits)
|
158 |
|
159 |
\A pass the PCRE_ANCHORED option to pcre_exec()
|
160 |
\B pass the PCRE_NOTBOL option to pcre_exec()
|
161 |
\E pass the PCRE_DOLLAR_ENDONLY option to pcre_exec()
|
162 |
\I pass the PCRE_CASELESS option to pcre_exec()
|
163 |
\M pass the PCRE_MULTILINE option to pcre_exec()
|
164 |
\S pass the PCRE_DOTALL option to pcre_exec()
|
165 |
\Odd set the size of the output vector passed to pcre_exec() to dd
|
166 |
(any number of decimal digits)
|
167 |
\Z pass the PCRE_NOTEOL option to pcre_exec()
|
168 |
|
169 |
A backslash followed by anything else just escapes the anything else. If the
|
170 |
very last character is a backslash, it is ignored. This gives a way of passing
|
171 |
an empty line as data, since a real empty line terminates the data input.
|
172 |
|
173 |
If /P was present on the regex, causing the POSIX wrapper API to be used, only
|
174 |
\B, and \Z have any effect, causing REG_NOTBOL and REG_NOTEOL to be passed to
|
175 |
regexec() respectively.
|
176 |
|
177 |
When a match succeeds, pcretest outputs the list of identified substrings that
|
178 |
pcre_exec() returns, starting with number 0 for the string that matched the
|
179 |
whole pattern. Here is an example of an interactive pcretest run.
|
180 |
|
181 |
$ pcretest
|
182 |
Testing Perl-Compatible Regular Expressions
|
183 |
PCRE version 0.90 08-Sep-1997
|
184 |
|
185 |
re> /^abc(\d+)/
|
186 |
data> abc123
|
187 |
0: abc123
|
188 |
1: 123
|
189 |
data> xyz
|
190 |
No match
|
191 |
|
192 |
Note that while patterns can be continued over several lines (a plain ">"
|
193 |
prompt is used for continuations), data lines may not. However newlines can be
|
194 |
included in data by means of the \n escape.
|
195 |
|
196 |
If the -p option is given to pcretest, it is equivalent to adding /P to each
|
197 |
regular expression: the POSIX wrapper API is used to call PCRE. None of the
|
198 |
following flags has any effect in this case.
|
199 |
|
200 |
If the option -d is given to pcretest, it is equivalent to adding /D to each
|
201 |
regular expression: the internal form is output after compilation.
|
202 |
|
203 |
If the option -i (for "information") is given to pcretest, it calls pcre_info()
|
204 |
after compiling an expression, and outputs the information it gets back. If the
|
205 |
pattern is studied, the results of that are also output.
|
206 |
|
207 |
If the option -s is given to pcretest, it outputs the size of each compiled
|
208 |
pattern after it has been compiled.
|
209 |
|
210 |
If the -t option is given, each compile, study, and match is run 2000 times
|
211 |
while being timed, and the resulting time per compile or match is output in
|
212 |
milliseconds. Do not set -t with -s, because you will then get the size output
|
213 |
2000 times and the timing will be distorted.
|
214 |
|
215 |
|
216 |
|
217 |
The perltest program
|
218 |
--------------------
|
219 |
|
220 |
The perltest program tests Perl's regular expressions; it has the same
|
221 |
specification as pcretest, and so can be given identical input, except that
|
222 |
input patterns can be followed only by Perl's lower case options.
|
223 |
|
224 |
The data lines are processed as Perl strings, so if they contain $ or @
|
225 |
characters, these have to be escaped. For this reason, all such characters in
|
226 |
the testinput file are escaped so that it can be used for perltest as well as
|
227 |
for pcretest, and the special upper case options such as /A that pcretest
|
228 |
recognizes are not used in this file. The output should be identical, apart
|
229 |
from the initial identifying banner.
|
230 |
|
231 |
The testinput2 file is not suitable for feeding to Perltest, since it does
|
232 |
make use of the special upper case options and escapes that pcretest uses to
|
233 |
test additional features of PCRE.
|
234 |
|
235 |
Philip Hazel <ph10@cam.ac.uk>
|
236 |
October 1997
|