1 |
README file for PCRE (Perl-compatible regular expressions)
|
2 |
----------------------------------------------------------
|
3 |
|
4 |
*******************************************************************************
|
5 |
* IMPORTANT FOR THOSE UPGRADING FROM VERSIONS BEFORE 2.00 *
|
6 |
* *
|
7 |
* Please note that there has been a change in the API such that a larger *
|
8 |
* ovector is required at matching time, to provide some additional workspace. *
|
9 |
* The new man page has details. This change was necessary in order to support *
|
10 |
* some of the new functionality in Perl 5.005. *
|
11 |
* *
|
12 |
* IMPORTANT FOR THOSE UPGRADING FROM VERSION 2.00 *
|
13 |
* *
|
14 |
* Another (I hope this is the last!) change has been made to the API for the *
|
15 |
* pcre_compile() function. An additional argument has been added to make it *
|
16 |
* possible to pass over a pointer to character tables built in the current *
|
17 |
* locale by pcre_maketables(). To use the default tables, this new arguement *
|
18 |
* should be passed as NULL. *
|
19 |
* *
|
20 |
* IMPORTANT FOR THOSE UPGRADING FROM VERSION 2.05 *
|
21 |
* *
|
22 |
* Yet another (and again I hope this really is the last) change has been made *
|
23 |
* to the API for the pcre_exec() function. An additional argument has been *
|
24 |
* added to make it possible to start the match other than at the start of the *
|
25 |
* subject string. This is important if there are lookbehinds. The new man *
|
26 |
* page has the details, but you just want to convert existing programs, all *
|
27 |
* you need to do is to stick in a new fifth argument to pcre_exec(), with a *
|
28 |
* value of zero. For example, change *
|
29 |
* *
|
30 |
* pcre_exec(pattern, extra, subject, length, options, ovec, ovecsize) *
|
31 |
* to *
|
32 |
* pcre_exec(pattern, extra, subject, length, 0, options, ovec, ovecsize) *
|
33 |
*******************************************************************************
|
34 |
|
35 |
|
36 |
The distribution should contain the following files:
|
37 |
|
38 |
ChangeLog log of changes to the code
|
39 |
LICENCE conditions for the use of PCRE
|
40 |
Makefile for building PCRE
|
41 |
README this file
|
42 |
RunTest a shell script for running tests
|
43 |
Tech.Notes notes on the encoding
|
44 |
pcre.3 man page for the functions
|
45 |
pcreposix.3 man page for the POSIX wrapper API
|
46 |
dftables.c auxiliary program for building chartables.c
|
47 |
get.c )
|
48 |
maketables.c )
|
49 |
study.c ) source of
|
50 |
pcre.c ) the functions
|
51 |
pcreposix.c )
|
52 |
pcre.h header for the external API
|
53 |
pcreposix.h header for the external POSIX wrapper API
|
54 |
internal.h header for internal use
|
55 |
pcretest.c test program
|
56 |
pgrep.1 man page for pgrep
|
57 |
pgrep.c source of a grep utility that uses PCRE
|
58 |
perltest Perl test program
|
59 |
testinput1 test data, compatible with Perl 5.004 and 5.005
|
60 |
testinput2 test data for error messages and non-Perl things
|
61 |
testinput3 test data, compatible with Perl 5.005
|
62 |
testinput4 test data for locale-specific tests
|
63 |
testoutput1 test results corresponding to testinput1
|
64 |
testoutput2 test results corresponding to testinput2
|
65 |
testoutput3 test results corresponding to testinput3
|
66 |
testoutput4 test results corresponding to testinput4
|
67 |
|
68 |
To build PCRE, edit Makefile for your system (it is a fairly simple make file,
|
69 |
and there are some comments at the top) and then run it. It builds two
|
70 |
libraries called libpcre.a and libpcreposix.a, a test program called pcretest,
|
71 |
and the pgrep command.
|
72 |
|
73 |
To test PCRE, run the RunTest script in the pcre directory. This runs pcretest
|
74 |
on each of the testinput files in turn, and compares the output with the
|
75 |
contents of the corresponding testoutput file. A file called testtry is used to
|
76 |
hold the output from pcretest (which is documented below).
|
77 |
|
78 |
To run pcretest on just one of the test files, give its number as an argument
|
79 |
to RunTest, for example:
|
80 |
|
81 |
RunTest 3
|
82 |
|
83 |
The first and third test files can also be fed directly into the perltest
|
84 |
program to check that Perl gives the same results. The third file requires the
|
85 |
additional features of release 5.005, which is why it is kept separate from the
|
86 |
main test input, which needs only Perl 5.004. In the long run, when 5.005 is
|
87 |
widespread, these two test files may get amalgamated.
|
88 |
|
89 |
The second set of tests check pcre_info(), pcre_study(), pcre_copy_substring(),
|
90 |
pcre_get_substring(), pcre_get_substring_list(), error detection and run-time
|
91 |
flags that are specific to PCRE, as well as the POSIX wrapper API.
|
92 |
|
93 |
The fourth set of tests checks pcre_maketables(), the facility for building a
|
94 |
set of character tables for a specific locale and using them instead of the
|
95 |
default tables. The tests make use of the "fr" (French) locale. Before running
|
96 |
the test, the script checks for the presence of this locale by running the
|
97 |
"locale" command. If that command fails, or if it doesn't include "fr" in the
|
98 |
list of available locales, the fourth test cannot be run, and a comment is
|
99 |
output to say why. If running this test produces instances of the error
|
100 |
|
101 |
** Failed to set locale "fr"
|
102 |
|
103 |
in the comparison output, it means that locale is not available on your system,
|
104 |
despite being listed by "locale". This does not mean that PCRE is broken.
|
105 |
|
106 |
To install PCRE, copy libpcre.a to any suitable library directory (e.g.
|
107 |
/usr/local/lib), pcre.h to any suitable include directory (e.g.
|
108 |
/usr/local/include), and pcre.3 to any suitable man directory (e.g.
|
109 |
/usr/local/man/man3).
|
110 |
|
111 |
To install the pgrep command, copy it to any suitable binary directory, (e.g.
|
112 |
/usr/local/bin) and pgrep.1 to any suitable man directory (e.g.
|
113 |
/usr/local/man/man1).
|
114 |
|
115 |
PCRE has its own native API, but a set of "wrapper" functions that are based on
|
116 |
the POSIX API are also supplied in the library libpcreposix.a. Note that this
|
117 |
just provides a POSIX calling interface to PCRE: the regular expressions
|
118 |
themselves still follow Perl syntax and semantics. The header file
|
119 |
for the POSIX-style functions is called pcreposix.h. The official POSIX name is
|
120 |
regex.h, but I didn't want to risk possible problems with existing files of
|
121 |
that name by distributing it that way. To use it with an existing program that
|
122 |
uses the POSIX API, it will have to be renamed or pointed at by a link.
|
123 |
|
124 |
|
125 |
Character tables
|
126 |
----------------
|
127 |
|
128 |
PCRE uses four tables for manipulating and identifying characters. The final
|
129 |
argument of the pcre_compile() function is a pointer to a block of memory
|
130 |
containing the concatenated tables. A call to pcre_maketables() can be used to
|
131 |
generate a set of tables in the current locale. If the final argument for
|
132 |
pcre_compile() is passed as NULL, a set of default tables that is built into
|
133 |
the binary is used.
|
134 |
|
135 |
The source file called chartables.c contains the default set of tables. This is
|
136 |
not supplied in the distribution, but is built by the program dftables
|
137 |
(compiled from dftables.c), which uses the ANSI C character handling functions
|
138 |
such as isalnum(), isalpha(), isupper(), islower(), etc. to build the table
|
139 |
sources. This means that the default C locale which is set for your system will
|
140 |
control the contents of these default tables. You can change the default tables
|
141 |
by editing chartables.c and then re-building PCRE. If you do this, you should
|
142 |
probably also edit Makefile to ensure that the file doesn't ever get
|
143 |
re-generated.
|
144 |
|
145 |
The first two 256-byte tables provide lower casing and case flipping functions,
|
146 |
respectively. The next table consists of three 32-byte bit maps which identify
|
147 |
digits, "word" characters, and white space, respectively. These are used when
|
148 |
building 32-byte bit maps that represent character classes.
|
149 |
|
150 |
The final 256-byte table has bits indicating various character types, as
|
151 |
follows:
|
152 |
|
153 |
1 white space character
|
154 |
2 letter
|
155 |
4 decimal digit
|
156 |
8 hexadecimal digit
|
157 |
16 alphanumeric or '_'
|
158 |
128 regular expression metacharacter or binary zero
|
159 |
|
160 |
You should not alter the set of characters that contain the 128 bit, as that
|
161 |
will cause PCRE to malfunction.
|
162 |
|
163 |
|
164 |
The pcretest program
|
165 |
--------------------
|
166 |
|
167 |
This program is intended for testing PCRE, but it can also be used for
|
168 |
experimenting with regular expressions.
|
169 |
|
170 |
If it is given two filename arguments, it reads from the first and writes to
|
171 |
the second. If it is given only one filename argument, it reads from that file
|
172 |
and writes to stdout. Otherwise, it reads from stdin and writes to stdout, and
|
173 |
prompts for each line of input.
|
174 |
|
175 |
The program handles any number of sets of input on a single input file. Each
|
176 |
set starts with a regular expression, and continues with any number of data
|
177 |
lines to be matched against the pattern. An empty line signals the end of the
|
178 |
set. The regular expressions are given enclosed in any non-alphameric
|
179 |
delimiters other than backslash, for example
|
180 |
|
181 |
/(a|bc)x+yz/
|
182 |
|
183 |
White space before the initial delimiter is ignored. A regular expression may
|
184 |
be continued over several input lines, in which case the newline characters are
|
185 |
included within it. See the testinput files for many examples. It is possible
|
186 |
to include the delimiter within the pattern by escaping it, for example
|
187 |
|
188 |
/abc\/def/
|
189 |
|
190 |
If you do so, the escape and the delimiter form part of the pattern, but since
|
191 |
delimiters are always non-alphameric, this does not affect its interpretation.
|
192 |
If the terminating delimiter is immediately followed by a backslash, for
|
193 |
example,
|
194 |
|
195 |
/abc/\
|
196 |
|
197 |
then a backslash is added to the end of the pattern. This is done to provide a
|
198 |
way of testing the error condition that arises if a pattern finishes with a
|
199 |
backslash, because
|
200 |
|
201 |
/abc\/
|
202 |
|
203 |
is interpreted as the first line of a pattern that starts with "abc/", causing
|
204 |
pcretest to read the next line as a continuation of the regular expression.
|
205 |
|
206 |
The pattern may be followed by i, m, s, or x to set the PCRE_CASELESS,
|
207 |
PCRE_MULTILINE, PCRE_DOTALL, or PCRE_EXTENDED options, respectively. For
|
208 |
example:
|
209 |
|
210 |
/caseless/i
|
211 |
|
212 |
These modifier letters have the same effect as they do in Perl. There are
|
213 |
others which set PCRE options that do not correspond to anything in Perl: /A,
|
214 |
/E, and /X set PCRE_ANCHORED, PCRE_DOLLAR_ENDONLY, and PCRE_EXTRA respectively.
|
215 |
|
216 |
Searching for all possible matches within each subject string can be requested
|
217 |
by the /g or /G modifier. The /g modifier behaves similarly to the way it does
|
218 |
in Perl. After finding a match, PCRE is called again to search the remainder of
|
219 |
the subject string. The difference between /g and /G is that the former uses
|
220 |
the start_offset argument to pcre_exec() to start searching at a new point
|
221 |
within the entire string, whereas the latter passes over a shortened substring.
|
222 |
This makes a difference to the matching process if the pattern begins with a
|
223 |
lookbehind assertion (including \b or \B).
|
224 |
|
225 |
There are a number of other modifiers for controlling the way pcretest
|
226 |
operates.
|
227 |
|
228 |
The /+ modifier requests that as well as outputting the substring that matched
|
229 |
the entire pattern, pcretest should in addition output the remainder of the
|
230 |
subject string. This is useful for tests where the subject contains multiple
|
231 |
copies of the same substring.
|
232 |
|
233 |
The /L modifier must be followed directly by the name of a locale, for example,
|
234 |
|
235 |
/pattern/Lfr
|
236 |
|
237 |
For this reason, it must be the last modifier letter. The given locale is set,
|
238 |
pcre_maketables() is called to build a set of character tables for the locale,
|
239 |
and this is then passed to pcre_compile() when compiling the regular
|
240 |
expression. Without an /L modifier, NULL is passed as the tables pointer; that
|
241 |
is, /L applies only to the expression on which it appears.
|
242 |
|
243 |
The /I modifier requests that pcretest output information about the compiled
|
244 |
expression (whether it is anchored, has a fixed first character, and so on). It
|
245 |
does this by calling pcre_info() after compiling an expression, and outputting
|
246 |
the information it gets back. If the pattern is studied, the results of that
|
247 |
are also output.
|
248 |
|
249 |
The /D modifier is a PCRE debugging feature, which also assumes /I. It causes
|
250 |
the internal form of compiled regular expressions to be output after
|
251 |
compilation.
|
252 |
|
253 |
The /S modifier causes pcre_study() to be called after the expression has been
|
254 |
compiled, and the results used when the expression is matched.
|
255 |
|
256 |
The /M modifier causes information about the size of memory block used to hold
|
257 |
the compile pattern to be output.
|
258 |
|
259 |
Finally, the /P modifier causes pcretest to call PCRE via the POSIX wrapper API
|
260 |
rather than its native API. When this is done, all other modifiers except /i,
|
261 |
/m, and /+ are ignored. REG_ICASE is set if /i is present, and REG_NEWLINE is
|
262 |
set if /m is present. The wrapper functions force PCRE_DOLLAR_ENDONLY always,
|
263 |
and PCRE_DOTALL unless REG_NEWLINE is set.
|
264 |
|
265 |
Before each data line is passed to pcre_exec(), leading and trailing whitespace
|
266 |
is removed, and it is then scanned for \ escapes. The following are recognized:
|
267 |
|
268 |
\a alarm (= BEL)
|
269 |
\b backspace
|
270 |
\e escape
|
271 |
\f formfeed
|
272 |
\n newline
|
273 |
\r carriage return
|
274 |
\t tab
|
275 |
\v vertical tab
|
276 |
\nnn octal character (up to 3 octal digits)
|
277 |
\xhh hexadecimal character (up to 2 hex digits)
|
278 |
|
279 |
\A pass the PCRE_ANCHORED option to pcre_exec()
|
280 |
\B pass the PCRE_NOTBOL option to pcre_exec()
|
281 |
\Cdd call pcre_copy_substring() for substring dd after a successful match
|
282 |
(any decimal number less than 32)
|
283 |
\Gdd call pcre_get_substring() for substring dd after a successful match
|
284 |
(any decimal number less than 32)
|
285 |
\L call pcre_get_substringlist() after a successful match
|
286 |
\Odd set the size of the output vector passed to pcre_exec() to dd
|
287 |
(any number of decimal digits)
|
288 |
\Z pass the PCRE_NOTEOL option to pcre_exec()
|
289 |
|
290 |
A backslash followed by anything else just escapes the anything else. If the
|
291 |
very last character is a backslash, it is ignored. This gives a way of passing
|
292 |
an empty line as data, since a real empty line terminates the data input.
|
293 |
|
294 |
If /P was present on the regex, causing the POSIX wrapper API to be used, only
|
295 |
\B, and \Z have any effect, causing REG_NOTBOL and REG_NOTEOL to be passed to
|
296 |
regexec() respectively.
|
297 |
|
298 |
When a match succeeds, pcretest outputs the list of captured substrings that
|
299 |
pcre_exec() returns, starting with number 0 for the string that matched the
|
300 |
whole pattern. Here is an example of an interactive pcretest run.
|
301 |
|
302 |
$ pcretest
|
303 |
PCRE version 2.06 08-Jun-1999
|
304 |
|
305 |
re> /^abc(\d+)/
|
306 |
data> abc123
|
307 |
0: abc123
|
308 |
1: 123
|
309 |
data> xyz
|
310 |
No match
|
311 |
|
312 |
If the strings contain any non-printing characters, they are output as \0x
|
313 |
escapes. If the pattern has the /+ modifier, then the output for substring 0 is
|
314 |
followed by the the rest of the subject string, identified by "0+" like this:
|
315 |
|
316 |
re> /cat/+
|
317 |
data> cataract
|
318 |
0: cat
|
319 |
0+ aract
|
320 |
|
321 |
If the pattern has the /g or /G modifier, the results of successive matching
|
322 |
attempts are output in sequence, like this:
|
323 |
|
324 |
re> /\Bi(\w\w)/g
|
325 |
data> Mississippi
|
326 |
0: iss
|
327 |
1: ss
|
328 |
0: iss
|
329 |
1: ss
|
330 |
0: ipp
|
331 |
1: pp
|
332 |
|
333 |
"No match" is output only if the first match attempt fails.
|
334 |
|
335 |
If any of \C, \G, or \L are present in a data line that is successfully
|
336 |
matched, the substrings extracted by the convenience functions are output with
|
337 |
C, G, or L after the string number instead of a colon. This is in addition to
|
338 |
the normal full list. The string length (that is, the return from the
|
339 |
extraction function) is given in parentheses after each string for \C and \G.
|
340 |
|
341 |
Note that while patterns can be continued over several lines (a plain ">"
|
342 |
prompt is used for continuations), data lines may not. However newlines can be
|
343 |
included in data by means of the \n escape.
|
344 |
|
345 |
If the -p option is given to pcretest, it is equivalent to adding /P to each
|
346 |
regular expression: the POSIX wrapper API is used to call PCRE. None of the
|
347 |
following flags has any effect in this case.
|
348 |
|
349 |
If the option -d is given to pcretest, it is equivalent to adding /D to each
|
350 |
regular expression: the internal form is output after compilation.
|
351 |
|
352 |
If the option -i is given to pcretest, it is equivalent to adding /I to each
|
353 |
regular expression: information about the compiled pattern is given after
|
354 |
compilation.
|
355 |
|
356 |
If the option -m is given to pcretest, it outputs the size of each compiled
|
357 |
pattern after it has been compiled. It is equivalent to adding /M to each
|
358 |
regular expression. For compatibility with earlier versions of pcretest, -s is
|
359 |
a synonym for -m.
|
360 |
|
361 |
If the -t option is given, each compile, study, and match is run 20000 times
|
362 |
while being timed, and the resulting time per compile or match is output in
|
363 |
milliseconds. Do not set -t with -s, because you will then get the size output
|
364 |
20000 times and the timing will be distorted. If you want to change the number
|
365 |
of repetitions used for timing, edit the definition of LOOPREPEAT at the top of
|
366 |
pcretest.c
|
367 |
|
368 |
|
369 |
|
370 |
The perltest program
|
371 |
--------------------
|
372 |
|
373 |
The perltest program tests Perl's regular expressions; it has the same
|
374 |
specification as pcretest, and so can be given identical input, except that
|
375 |
input patterns can be followed only by Perl's lower case modifiers. The
|
376 |
contents of testinput1 and testinput3 meet this condition.
|
377 |
|
378 |
The data lines are processed as Perl strings, so if they contain $ or @
|
379 |
characters, these have to be escaped. For this reason, all such characters in
|
380 |
testinput1 and testinput3 are escaped so that they can be used for perltest as
|
381 |
well as for pcretest, and the special upper case modifiers such as /A that
|
382 |
pcretest recognizes are not used in these files. The output should be
|
383 |
identical, apart from the initial identifying banner.
|
384 |
|
385 |
The testinput2 and testinput4 files are not suitable for feeding to perltest,
|
386 |
since they do make use of the special upper case modifiers and escapes that
|
387 |
pcretest uses to test some features of PCRE. The first of these files also
|
388 |
contains malformed regular expressions, in order to check that PCRE diagnoses
|
389 |
them correctly.
|
390 |
|
391 |
Philip Hazel <ph10@cam.ac.uk>
|
392 |
June 1999
|