1 |
.TH PCRETEST 1
|
2 |
.SH NAME
|
3 |
pcretest - a program for testing Perl-compatible regular expressions.
|
4 |
.SH SYNOPSIS
|
5 |
.B pcretest "[-d] [-i] [-m] [-o osize] [-p] [-t] [source] [destination]"
|
6 |
|
7 |
\fBpcretest\fR was written as a test program for the PCRE regular expression
|
8 |
library itself, but it can also be used for experimenting with regular
|
9 |
expressions. This document describes the features of the test program; for
|
10 |
details of the regular expressions themselves, see the
|
11 |
.\" HREF
|
12 |
\fBpcrepattern\fR
|
13 |
.\"
|
14 |
documentation. For details of PCRE and its options, see the
|
15 |
.\" HREF
|
16 |
\fBpcreapi\fR
|
17 |
.\"
|
18 |
documentation.
|
19 |
|
20 |
.SH OPTIONS
|
21 |
.rs
|
22 |
.sp
|
23 |
.TP 10
|
24 |
\fB-C\fR
|
25 |
Output the version number of the PCRE library, and all available information
|
26 |
about the optional features that are included, and then exit.
|
27 |
.TP 10
|
28 |
\fB-d\fR
|
29 |
Behave as if each regex had the \fB/D\fR modifier (see below); the internal
|
30 |
form is output after compilation.
|
31 |
.TP 10
|
32 |
\fB-i\fR
|
33 |
Behave as if each regex had the \fB/I\fR modifier; information about the
|
34 |
compiled pattern is given after compilation.
|
35 |
.TP 10
|
36 |
\fB-m\fR
|
37 |
Output the size of each compiled pattern after it has been compiled. This is
|
38 |
equivalent to adding /M to each regular expression. For compatibility with
|
39 |
earlier versions of pcretest, \fB-s\fR is a synonym for \fB-m\fR.
|
40 |
.TP 10
|
41 |
\fB-o\fR \fIosize\fR
|
42 |
Set the number of elements in the output vector that is used when calling PCRE
|
43 |
to be \fIosize\fR. The default value is 45, which is enough for 14 capturing
|
44 |
subexpressions. The vector size can be changed for individual matching calls by
|
45 |
including \\O in the data line (see below).
|
46 |
.TP 10
|
47 |
\fB-p\fR
|
48 |
Behave as if each regex has \fB/P\fR modifier; the POSIX wrapper API is used
|
49 |
to call PCRE. None of the other options has any effect when \fB-p\fR is set.
|
50 |
.TP 10
|
51 |
\fB-t\fR
|
52 |
Run each compile, study, and match many times with a timer, and output
|
53 |
resulting time per compile or match (in milliseconds). Do not set \fB-t\fR with
|
54 |
\fB-m\fR, because you will then get the size output 20000 times and the timing
|
55 |
will be distorted.
|
56 |
|
57 |
.SH DESCRIPTION
|
58 |
.rs
|
59 |
.sp
|
60 |
If \fBpcretest\fR is given two filename arguments, it reads from the first and
|
61 |
writes to the second. If it is given only one filename argument, it reads from
|
62 |
that file and writes to stdout. Otherwise, it reads from stdin and writes to
|
63 |
stdout, and prompts for each line of input, using "re>" to prompt for regular
|
64 |
expressions, and "data>" to prompt for data lines.
|
65 |
|
66 |
The program handles any number of sets of input on a single input file. Each
|
67 |
set starts with a regular expression, and continues with any number of data
|
68 |
lines to be matched against the pattern.
|
69 |
|
70 |
Each line is matched separately and independently. If you want to do
|
71 |
multiple-line matches, you have to use the \\n escape sequence in a single line
|
72 |
of input to encode the newline characters. The maximum length of data line is
|
73 |
30,000 characters.
|
74 |
|
75 |
An empty line signals the end of the data lines, at which point a new regular
|
76 |
expression is read. The regular expressions are given enclosed in any
|
77 |
non-alphameric delimiters other than backslash, for example
|
78 |
|
79 |
/(a|bc)x+yz/
|
80 |
|
81 |
White space before the initial delimiter is ignored. A regular expression may
|
82 |
be continued over several input lines, in which case the newline characters are
|
83 |
included within it. It is possible to include the delimiter within the pattern
|
84 |
by escaping it, for example
|
85 |
|
86 |
/abc\\/def/
|
87 |
|
88 |
If you do so, the escape and the delimiter form part of the pattern, but since
|
89 |
delimiters are always non-alphameric, this does not affect its interpretation.
|
90 |
If the terminating delimiter is immediately followed by a backslash, for
|
91 |
example,
|
92 |
|
93 |
/abc/\\
|
94 |
|
95 |
then a backslash is added to the end of the pattern. This is done to provide a
|
96 |
way of testing the error condition that arises if a pattern finishes with a
|
97 |
backslash, because
|
98 |
|
99 |
/abc\\/
|
100 |
|
101 |
is interpreted as the first line of a pattern that starts with "abc/", causing
|
102 |
pcretest to read the next line as a continuation of the regular expression.
|
103 |
|
104 |
.SH PATTERN MODIFIERS
|
105 |
.rs
|
106 |
.sp
|
107 |
The pattern may be followed by \fBi\fR, \fBm\fR, \fBs\fR, or \fBx\fR to set the
|
108 |
PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, or PCRE_EXTENDED options,
|
109 |
respectively. For example:
|
110 |
|
111 |
/caseless/i
|
112 |
|
113 |
These modifier letters have the same effect as they do in Perl. There are
|
114 |
others which set PCRE options that do not correspond to anything in Perl:
|
115 |
\fB/A\fR, \fB/E\fR, and \fB/X\fR set PCRE_ANCHORED, PCRE_DOLLAR_ENDONLY, and
|
116 |
PCRE_EXTRA respectively.
|
117 |
|
118 |
Searching for all possible matches within each subject string can be requested
|
119 |
by the \fB/g\fR or \fB/G\fR modifier. After finding a match, PCRE is called
|
120 |
again to search the remainder of the subject string. The difference between
|
121 |
\fB/g\fR and \fB/G\fR is that the former uses the \fIstartoffset\fR argument to
|
122 |
\fBpcre_exec()\fR to start searching at a new point within the entire string
|
123 |
(which is in effect what Perl does), whereas the latter passes over a shortened
|
124 |
substring. This makes a difference to the matching process if the pattern
|
125 |
begins with a lookbehind assertion (including \\b or \\B).
|
126 |
|
127 |
If any call to \fBpcre_exec()\fR in a \fB/g\fR or \fB/G\fR sequence matches an
|
128 |
empty string, the next call is done with the PCRE_NOTEMPTY and PCRE_ANCHORED
|
129 |
flags set in order to search for another, non-empty, match at the same point.
|
130 |
If this second match fails, the start offset is advanced by one, and the normal
|
131 |
match is retried. This imitates the way Perl handles such cases when using the
|
132 |
\fB/g\fR modifier or the \fBsplit()\fR function.
|
133 |
|
134 |
There are a number of other modifiers for controlling the way \fBpcretest\fR
|
135 |
operates.
|
136 |
|
137 |
The \fB/+\fR modifier requests that as well as outputting the substring that
|
138 |
matched the entire pattern, pcretest should in addition output the remainder of
|
139 |
the subject string. This is useful for tests where the subject contains
|
140 |
multiple copies of the same substring.
|
141 |
|
142 |
The \fB/L\fR modifier must be followed directly by the name of a locale, for
|
143 |
example,
|
144 |
|
145 |
/pattern/Lfr
|
146 |
|
147 |
For this reason, it must be the last modifier letter. The given locale is set,
|
148 |
\fBpcre_maketables()\fR is called to build a set of character tables for the
|
149 |
locale, and this is then passed to \fBpcre_compile()\fR when compiling the
|
150 |
regular expression. Without an \fB/L\fR modifier, NULL is passed as the tables
|
151 |
pointer; that is, \fB/L\fR applies only to the expression on which it appears.
|
152 |
|
153 |
The \fB/I\fR modifier requests that \fBpcretest\fR output information about the
|
154 |
compiled expression (whether it is anchored, has a fixed first character, and
|
155 |
so on). It does this by calling \fBpcre_fullinfo()\fR after compiling an
|
156 |
expression, and outputting the information it gets back. If the pattern is
|
157 |
studied, the results of that are also output.
|
158 |
|
159 |
The \fB/D\fR modifier is a PCRE debugging feature, which also assumes \fB/I\fR.
|
160 |
It causes the internal form of compiled regular expressions to be output after
|
161 |
compilation. If the pattern was studied, the information returned is also
|
162 |
output.
|
163 |
|
164 |
The \fB/S\fR modifier causes \fBpcre_study()\fR to be called after the
|
165 |
expression has been compiled, and the results used when the expression is
|
166 |
matched.
|
167 |
|
168 |
The \fB/M\fR modifier causes the size of memory block used to hold the compiled
|
169 |
pattern to be output.
|
170 |
|
171 |
The \fB/P\fR modifier causes \fBpcretest\fR to call PCRE via the POSIX wrapper
|
172 |
API rather than its native API. When this is done, all other modifiers except
|
173 |
\fB/i\fR, \fB/m\fR, and \fB/+\fR are ignored. REG_ICASE is set if \fB/i\fR is
|
174 |
present, and REG_NEWLINE is set if \fB/m\fR is present. The wrapper functions
|
175 |
force PCRE_DOLLAR_ENDONLY always, and PCRE_DOTALL unless REG_NEWLINE is set.
|
176 |
|
177 |
The \fB/8\fR modifier causes \fBpcretest\fR to call PCRE with the PCRE_UTF8
|
178 |
option set. This turns on support for UTF-8 character handling in PCRE,
|
179 |
provided that it was compiled with this support enabled. This modifier also
|
180 |
causes any non-printing characters in output strings to be printed using the
|
181 |
\\x{hh...} notation if they are valid UTF-8 sequences.
|
182 |
|
183 |
.SH CALLOUTS
|
184 |
.rs
|
185 |
.sp
|
186 |
If the pattern contains any callout requests, \fBpcretest\fR's callout function
|
187 |
will be called. By default, it displays the callout number, and the start and
|
188 |
current positions in the text at the callout time. For example, the output
|
189 |
|
190 |
--->pqrabcdef
|
191 |
0 ^ ^
|
192 |
|
193 |
indicates that callout number 0 occurred for a match attempt starting at the
|
194 |
fourth character of the subject string, when the pointer was at the seventh
|
195 |
character. The callout function returns zero (carry on matching) by default.
|
196 |
|
197 |
Inserting callouts may be helpful when using \fBpcretest\fR to check
|
198 |
complicated regular expressions. For further information about callouts, see
|
199 |
the
|
200 |
.\" HREF
|
201 |
\fBpcrecallout\fR
|
202 |
.\"
|
203 |
documentation.
|
204 |
|
205 |
For testing the PCRE library, additional control of callout behaviour is
|
206 |
available via escape sequences in the data, as described in the following
|
207 |
section. In particular, it is possible to pass in a number as callout data (the
|
208 |
default is zero). If the callout function receives a non-zero number, it
|
209 |
returns that value instead of zero.
|
210 |
|
211 |
.SH DATA LINES
|
212 |
.rs
|
213 |
.sp
|
214 |
Before each data line is passed to \fBpcre_exec()\fR, leading and trailing
|
215 |
whitespace is removed, and it is then scanned for \\ escapes. Some of these are
|
216 |
pretty esoteric features, intended for checking out some of the more
|
217 |
complicated features of PCRE. If you are just testing "ordinary" regular
|
218 |
expressions, you probably don't need any of these. The following escapes are
|
219 |
recognized:
|
220 |
|
221 |
\\a alarm (= BEL)
|
222 |
\\b backspace
|
223 |
\\e escape
|
224 |
\\f formfeed
|
225 |
\\n newline
|
226 |
\\r carriage return
|
227 |
\\t tab
|
228 |
\\v vertical tab
|
229 |
\\nnn octal character (up to 3 octal digits)
|
230 |
\\xhh hexadecimal character (up to 2 hex digits)
|
231 |
\\x{hh...} hexadecimal character, any number of digits
|
232 |
in UTF-8 mode
|
233 |
\\A pass the PCRE_ANCHORED option to \fBpcre_exec()\fR
|
234 |
\\B pass the PCRE_NOTBOL option to \fBpcre_exec()\fR
|
235 |
\\Cdd call pcre_copy_substring() for substring dd
|
236 |
after a successful match (any decimal number
|
237 |
less than 32)
|
238 |
\\Cname call pcre_copy_named_substring() for substring
|
239 |
"name" after a successful match (name termin-
|
240 |
ated by next non alphanumeric character)
|
241 |
\\C+ show the current captured substrings at callout
|
242 |
time
|
243 |
\\C- do not supply a callout function
|
244 |
\\C!n return 1 instead of 0 when callout number n is
|
245 |
reached
|
246 |
\\C!n!m return 1 instead of 0 when callout number n is
|
247 |
reached for the nth time
|
248 |
\\C*n pass the number n (may be negative) as callout
|
249 |
data
|
250 |
\\Gdd call pcre_get_substring() for substring dd
|
251 |
after a successful match (any decimal number
|
252 |
less than 32)
|
253 |
\\Gname call pcre_get_named_substring() for substring
|
254 |
"name" after a successful match (name termin-
|
255 |
ated by next non-alphanumeric character)
|
256 |
\\L call pcre_get_substringlist() after a
|
257 |
successful match
|
258 |
\\M discover the minimum MATCH_LIMIT setting
|
259 |
\\N pass the PCRE_NOTEMPTY option to \fBpcre_exec()\fR
|
260 |
\\Odd set the size of the output vector passed to
|
261 |
\fBpcre_exec()\fR to dd (any number of decimal
|
262 |
digits)
|
263 |
\\Z pass the PCRE_NOTEOL option to \fBpcre_exec()\fR
|
264 |
|
265 |
If \\M is present, \fBpcretest\fR calls \fBpcre_exec()\fR several times, with
|
266 |
different values in the \fImatch_limit\fR field of the \fBpcre_extra\fR data
|
267 |
structure, until it finds the minimum number that is needed for
|
268 |
\fBpcre_exec()\fR to complete. This number is a measure of the amount of
|
269 |
recursion and backtracking that takes place, and checking it out can be
|
270 |
instructive. For most simple matches, the number is quite small, but for
|
271 |
patterns with very large numbers of matching possibilities, it can become large
|
272 |
very quickly with increasing length of subject string.
|
273 |
|
274 |
When \\O is used, it may be higher or lower than the size set by the \fB-O\fR
|
275 |
option (or defaulted to 45); \\O applies only to the call of \fBpcre_exec()\fR
|
276 |
for the line in which it appears.
|
277 |
|
278 |
A backslash followed by anything else just escapes the anything else. If the
|
279 |
very last character is a backslash, it is ignored. This gives a way of passing
|
280 |
an empty line as data, since a real empty line terminates the data input.
|
281 |
|
282 |
If \fB/P\fR was present on the regex, causing the POSIX wrapper API to be used,
|
283 |
only \fB\B\fR, and \fB\Z\fR have any effect, causing REG_NOTBOL and REG_NOTEOL
|
284 |
to be passed to \fBregexec()\fR respectively.
|
285 |
|
286 |
The use of \\x{hh...} to represent UTF-8 characters is not dependent on the use
|
287 |
of the \fB/8\fR modifier on the pattern. It is recognized always. There may be
|
288 |
any number of hexadecimal digits inside the braces. The result is from one to
|
289 |
six bytes, encoded according to the UTF-8 rules.
|
290 |
|
291 |
.SH OUTPUT FROM PCRETEST
|
292 |
.rs
|
293 |
.sp
|
294 |
When a match succeeds, pcretest outputs the list of captured substrings that
|
295 |
\fBpcre_exec()\fR returns, starting with number 0 for the string that matched
|
296 |
the whole pattern. Here is an example of an interactive pcretest run.
|
297 |
|
298 |
$ pcretest
|
299 |
PCRE version 4.00 08-Jan-2003
|
300 |
|
301 |
re> /^abc(\\d+)/
|
302 |
data> abc123
|
303 |
0: abc123
|
304 |
1: 123
|
305 |
data> xyz
|
306 |
No match
|
307 |
|
308 |
If the strings contain any non-printing characters, they are output as \\0x
|
309 |
escapes, or as \\x{...} escapes if the \fB/8\fR modifier was present on the
|
310 |
pattern. If the pattern has the \fB/+\fR modifier, then the output for
|
311 |
substring 0 is followed by the the rest of the subject string, identified by
|
312 |
"0+" like this:
|
313 |
|
314 |
re> /cat/+
|
315 |
data> cataract
|
316 |
0: cat
|
317 |
0+ aract
|
318 |
|
319 |
If the pattern has the \fB/g\fR or \fB/G\fR modifier, the results of successive
|
320 |
matching attempts are output in sequence, like this:
|
321 |
|
322 |
re> /\\Bi(\\w\\w)/g
|
323 |
data> Mississippi
|
324 |
0: iss
|
325 |
1: ss
|
326 |
0: iss
|
327 |
1: ss
|
328 |
0: ipp
|
329 |
1: pp
|
330 |
|
331 |
"No match" is output only if the first match attempt fails.
|
332 |
|
333 |
If any of the sequences \fB\\C\fR, \fB\\G\fR, or \fB\\L\fR are present in a
|
334 |
data line that is successfully matched, the substrings extracted by the
|
335 |
convenience functions are output with C, G, or L after the string number
|
336 |
instead of a colon. This is in addition to the normal full list. The string
|
337 |
length (that is, the return from the extraction function) is given in
|
338 |
parentheses after each string for \fB\\C\fR and \fB\\G\fR.
|
339 |
|
340 |
Note that while patterns can be continued over several lines (a plain ">"
|
341 |
prompt is used for continuations), data lines may not. However newlines can be
|
342 |
included in data by means of the \\n escape.
|
343 |
|
344 |
.SH AUTHOR
|
345 |
.rs
|
346 |
.sp
|
347 |
Philip Hazel <ph10@cam.ac.uk>
|
348 |
.br
|
349 |
University Computing Service,
|
350 |
.br
|
351 |
Cambridge CB2 3QG, England.
|
352 |
|
353 |
.in 0
|
354 |
Last updated: 03 February 2003
|
355 |
.br
|
356 |
Copyright (c) 1997-2003 University of Cambridge.
|