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 |
|
21 |
The distribution should contain the following files:
|
22 |
|
23 |
ChangeLog log of changes to the code
|
24 |
Makefile for building PCRE
|
25 |
README this file
|
26 |
RunTest a shell script for running tests
|
27 |
Tech.Notes notes on the encoding
|
28 |
pcre.3 man page for the functions
|
29 |
pcreposix.3 man page for the POSIX wrapper API
|
30 |
deftables.c auxiliary program for building chartables.c
|
31 |
maketables.c )
|
32 |
study.c ) source of
|
33 |
pcre.c ) the functions
|
34 |
pcreposix.c )
|
35 |
pcre.h header for the external API
|
36 |
pcreposix.h header for the external POSIX wrapper API
|
37 |
internal.h header for internal use
|
38 |
pcretest.c test program
|
39 |
pgrep.1 man page for pgrep
|
40 |
pgrep.c source of a grep utility that uses PCRE
|
41 |
perltest Perl test program
|
42 |
testinput test data, compatible with Perl 5.004 and 5.005
|
43 |
testinput2 test data for error messages and non-Perl things
|
44 |
testinput3 test data, compatible with Perl 5.005
|
45 |
testinput4 test data for locale-specific tests
|
46 |
testoutput test results corresponding to testinput
|
47 |
testoutput2 test results corresponding to testinput2
|
48 |
testoutput3 test results corresponding to testinput3
|
49 |
testoutput4 test results corresponding to testinput4
|
50 |
|
51 |
To build PCRE, edit Makefile for your system (it is a fairly simple make file,
|
52 |
and there are some comments at the top) and then run it. It builds two
|
53 |
libraries called libpcre.a and libpcreposix.a, a test program called pcretest,
|
54 |
and the pgrep command.
|
55 |
|
56 |
To test PCRE, run the RunTest script in the pcre directory. This runs pcretest
|
57 |
on each of the testinput files in turn, and compares the output with the
|
58 |
contents of the corresponding testoutput file. A file called testtry is used to
|
59 |
hold the output from pcretest (which is documented below).
|
60 |
|
61 |
To run pcretest on just one of the test files, give its number as an argument
|
62 |
to RunTest, for example:
|
63 |
|
64 |
RunTest 3
|
65 |
|
66 |
The first and third test files can also be fed directly into the perltest
|
67 |
program to check that Perl gives the same results. The third file requires the
|
68 |
additional features of release 5.005, which is why it is kept separate from the
|
69 |
main test input, which needs only Perl 5.004. In the long run, when 5.005 is
|
70 |
widespread, these two test files may get amalgamated.
|
71 |
|
72 |
The second set of tests check pcre_info(), pcre_study(), error detection and
|
73 |
run-time flags that are specific to PCRE, as well as the POSIX wrapper API.
|
74 |
|
75 |
The fourth set of tests checks pcre_maketables(), the facility for building a
|
76 |
set of character tables for a specific locale and using them instead of the
|
77 |
default tables. The tests make use of the "fr" (French) locale. Before running
|
78 |
the test, the script checks for the presence of this locale by running the
|
79 |
"locale" command. If that command fails, or if it doesn't include "fr" in the
|
80 |
list of available locales, the fourth test cannot be run, and a comment is
|
81 |
output to say why. If running this test produces instances of the error
|
82 |
|
83 |
** Failed to set locale "fr"
|
84 |
|
85 |
in the comparison output, it means that locale is not available on your system,
|
86 |
despite being listed by "locale". This does not mean that PCRE is broken.
|
87 |
|
88 |
To install PCRE, copy libpcre.a to any suitable library directory (e.g.
|
89 |
/usr/local/lib), pcre.h to any suitable include directory (e.g.
|
90 |
/usr/local/include), and pcre.3 to any suitable man directory (e.g.
|
91 |
/usr/local/man/man3).
|
92 |
|
93 |
To install the pgrep command, copy it to any suitable binary directory, (e.g.
|
94 |
/usr/local/bin) and pgrep.1 to any suitable man directory (e.g.
|
95 |
/usr/local/man/man1).
|
96 |
|
97 |
PCRE has its own native API, but a set of "wrapper" functions that are based on
|
98 |
the POSIX API are also supplied in the library libpcreposix.a. Note that this
|
99 |
just provides a POSIX calling interface to PCRE: the regular expressions
|
100 |
themselves still follow Perl syntax and semantics. The header file
|
101 |
for the POSIX-style functions is called pcreposix.h. The official POSIX name is
|
102 |
regex.h, but I didn't want to risk possible problems with existing files of
|
103 |
that name by distributing it that way. To use it with an existing program that
|
104 |
uses the POSIX API, it will have to be renamed or pointed at by a link.
|
105 |
|
106 |
|
107 |
Character tables
|
108 |
----------------
|
109 |
|
110 |
PCRE uses four tables for manipulating and identifying characters. The final
|
111 |
argument of the pcre_compile() function is a pointer to a block of memory
|
112 |
containing the concatenated tables. A call to pcre_maketables() is used to
|
113 |
generate a set of tables in the current locale. However, if the final argument
|
114 |
is passed as NULL, a set of default tables that is built into the binary is
|
115 |
used.
|
116 |
|
117 |
The source file called chartables.c contains the default set of tables. This is
|
118 |
not supplied in the distribution, but is built by the program deftables
|
119 |
(compiled from deftables.c), which uses the ANSI C character handling functions
|
120 |
such as isalnum(), isalpha(), isupper(), islower(), etc. to build the table
|
121 |
sources. This means that the default C locale set your system will control the
|
122 |
contents of the tables. You can change the default tables by editing
|
123 |
chartables.c and then re-building PCRE. If you do this, you should probably
|
124 |
also edit Makefile to ensure that the file doesn't ever get re-generated.
|
125 |
|
126 |
The first two 256-byte tables provide lower casing and case flipping functions,
|
127 |
respectively. The next table consists of three 32-byte bit maps which identify
|
128 |
digits, "word" characters, and white space, respectively. These are used when
|
129 |
building 32-byte bit maps that represent character classes.
|
130 |
|
131 |
The final 256-byte table has bits indicating various character types, as
|
132 |
follows:
|
133 |
|
134 |
1 white space character
|
135 |
2 letter
|
136 |
4 decimal digit
|
137 |
8 hexadecimal digit
|
138 |
16 alphanumeric or '_'
|
139 |
128 regular expression metacharacter or binary zero
|
140 |
|
141 |
You should not alter the set of characters that contain the 128 bit, as that
|
142 |
will cause PCRE to malfunction.
|
143 |
|
144 |
|
145 |
The pcretest program
|
146 |
--------------------
|
147 |
|
148 |
This program is intended for testing PCRE, but it can also be used for
|
149 |
experimenting with regular expressions.
|
150 |
|
151 |
If it is given two filename arguments, it reads from the first and writes to
|
152 |
the second. If it is given only one filename argument, it reads from that file
|
153 |
and writes to stdout. Otherwise, it reads from stdin and writes to stdout, and
|
154 |
prompts for each line of input.
|
155 |
|
156 |
The program handles any number of sets of input on a single input file. Each
|
157 |
set starts with a regular expression, and continues with any number of data
|
158 |
lines to be matched against the pattern. An empty line signals the end of the
|
159 |
set. The regular expressions are given enclosed in any non-alphameric
|
160 |
delimiters, for example
|
161 |
|
162 |
/(a|bc)x+yz/
|
163 |
|
164 |
and may be followed by i, m, s, or x to set the PCRE_CASELESS, PCRE_MULTILINE,
|
165 |
PCRE_DOTALL, or PCRE_EXTENDED options, respectively. These options have the
|
166 |
same effect as they do in Perl.
|
167 |
|
168 |
There are also some upper case options that do not match Perl options: /A, /E,
|
169 |
and /X set PCRE_ANCHORED, PCRE_DOLLAR_ENDONLY, and PCRE_EXTRA respectively.
|
170 |
|
171 |
The /L option must be followed directly by the name of a locale, for example,
|
172 |
|
173 |
/pattern/Lfr
|
174 |
|
175 |
For this reason, it must be the last option letter. The given locale is set,
|
176 |
pcre_maketables() is called to build a set of character tables for the locale,
|
177 |
and this is then passed to pcre_compile() when compiling the regular
|
178 |
expression. Without an /L option, NULL is passed as the tables pointer; that
|
179 |
is, /L applies only to the expression on which it appears.
|
180 |
|
181 |
The /I option requests that pcretest output information about the compiled
|
182 |
expression (whether it is anchored, has a fixed first character, and so on). It
|
183 |
does this by calling pcre_info() after compiling an expression, and outputting
|
184 |
the information it gets back. If the pattern is studied, the results of that
|
185 |
are also output.
|
186 |
|
187 |
The /D option is a PCRE debugging feature, which also assumes /I. It causes the
|
188 |
internal form of compiled regular expressions to be output after compilation.
|
189 |
|
190 |
The /S option causes pcre_study() to be called after the expression has been
|
191 |
compiled, and the results used when the expression is matched.
|
192 |
|
193 |
Finally, the /P option causes pcretest to call PCRE via the POSIX wrapper API
|
194 |
rather than its native API. When this is done, all other options except /i and
|
195 |
/m are ignored. REG_ICASE is set if /i is present, and REG_NEWLINE is set if /m
|
196 |
is present. The wrapper functions force PCRE_DOLLAR_ENDONLY always, and
|
197 |
PCRE_DOTALL unless REG_NEWLINE is set.
|
198 |
|
199 |
A regular expression can extend over several lines of input; the newlines are
|
200 |
included in it. See the testinput files for many examples.
|
201 |
|
202 |
Before each data line is passed to pcre_exec(), leading and trailing whitespace
|
203 |
is removed, and it is then scanned for \ escapes. The following are recognized:
|
204 |
|
205 |
\a alarm (= BEL)
|
206 |
\b backspace
|
207 |
\e escape
|
208 |
\f formfeed
|
209 |
\n newline
|
210 |
\r carriage return
|
211 |
\t tab
|
212 |
\v vertical tab
|
213 |
\nnn octal character (up to 3 octal digits)
|
214 |
\xhh hexadecimal character (up to 2 hex digits)
|
215 |
|
216 |
\A pass the PCRE_ANCHORED option to pcre_exec()
|
217 |
\B pass the PCRE_NOTBOL option to pcre_exec()
|
218 |
\Odd set the size of the output vector passed to pcre_exec() to dd
|
219 |
(any number of decimal digits)
|
220 |
\Z pass the PCRE_NOTEOL option to pcre_exec()
|
221 |
|
222 |
A backslash followed by anything else just escapes the anything else. If the
|
223 |
very last character is a backslash, it is ignored. This gives a way of passing
|
224 |
an empty line as data, since a real empty line terminates the data input.
|
225 |
|
226 |
If /P was present on the regex, causing the POSIX wrapper API to be used, only
|
227 |
\B, and \Z have any effect, causing REG_NOTBOL and REG_NOTEOL to be passed to
|
228 |
regexec() respectively.
|
229 |
|
230 |
When a match succeeds, pcretest outputs the list of identified substrings that
|
231 |
pcre_exec() returns, starting with number 0 for the string that matched the
|
232 |
whole pattern. Here is an example of an interactive pcretest run.
|
233 |
|
234 |
$ pcretest
|
235 |
Testing Perl-Compatible Regular Expressions
|
236 |
PCRE version 0.90 08-Sep-1997
|
237 |
|
238 |
re> /^abc(\d+)/
|
239 |
data> abc123
|
240 |
0: abc123
|
241 |
1: 123
|
242 |
data> xyz
|
243 |
No match
|
244 |
|
245 |
Note that while patterns can be continued over several lines (a plain ">"
|
246 |
prompt is used for continuations), data lines may not. However newlines can be
|
247 |
included in data by means of the \n escape.
|
248 |
|
249 |
If the -p option is given to pcretest, it is equivalent to adding /P to each
|
250 |
regular expression: the POSIX wrapper API is used to call PCRE. None of the
|
251 |
following flags has any effect in this case.
|
252 |
|
253 |
If the option -d is given to pcretest, it is equivalent to adding /D to each
|
254 |
regular expression: the internal form is output after compilation.
|
255 |
|
256 |
If the option -i is given to pcretest, it is equivalent to adding /I to each
|
257 |
regular expression: information about the compiled pattern is given after
|
258 |
compilation.
|
259 |
|
260 |
If the option -s is given to pcretest, it outputs the size of each compiled
|
261 |
pattern after it has been compiled.
|
262 |
|
263 |
If the -t option is given, each compile, study, and match is run 10000 times
|
264 |
while being timed, and the resulting time per compile or match is output in
|
265 |
milliseconds. Do not set -t with -s, because you will then get the size output
|
266 |
10000 times and the timing will be distorted. If you want to change the number
|
267 |
of repetitions used for timing, edit the definition of LOOPREPEAT at the top of
|
268 |
pcretest.c
|
269 |
|
270 |
|
271 |
|
272 |
The perltest program
|
273 |
--------------------
|
274 |
|
275 |
The perltest program tests Perl's regular expressions; it has the same
|
276 |
specification as pcretest, and so can be given identical input, except that
|
277 |
input patterns can be followed only by Perl's lower case options. The contents
|
278 |
of testinput and testinput3 meet this condition.
|
279 |
|
280 |
The data lines are processed as Perl strings, so if they contain $ or @
|
281 |
characters, these have to be escaped. For this reason, all such characters in
|
282 |
the testinput file are escaped so that it can be used for perltest as well as
|
283 |
for pcretest, and the special upper case options such as /A that pcretest
|
284 |
recognizes are not used in this file. The output should be identical, apart
|
285 |
from the initial identifying banner.
|
286 |
|
287 |
The testinput2 and testinput4 files are not suitable for feeding to Perltest,
|
288 |
since they do make use of the special upper case options and escapes that
|
289 |
pcretest uses to test some features of PCRE. The first of these files also
|
290 |
contains malformed regular expressions, in order to check that PCRE diagnoses
|
291 |
them correctly.
|
292 |
|
293 |
Philip Hazel <ph10@cam.ac.uk>
|
294 |
October 1998
|