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