1 |
The pcretest program
|
2 |
--------------------
|
3 |
|
4 |
This program is intended for testing PCRE, but it can also be used for
|
5 |
experimenting with regular expressions.
|
6 |
|
7 |
If it is given two filename arguments, it reads from the first and writes to
|
8 |
the second. If it is given only one filename argument, it reads from that file
|
9 |
and writes to stdout. Otherwise, it reads from stdin and writes to stdout, and
|
10 |
prompts for each line of input, using "re>" to prompt for regular expressions,
|
11 |
and "data>" to prompt for data lines.
|
12 |
|
13 |
The program handles any number of sets of input on a single input file. Each
|
14 |
set starts with a regular expression, and continues with any number of data
|
15 |
lines to be matched against the pattern. An empty line signals the end of the
|
16 |
data lines, at which point a new regular expression is read. The regular
|
17 |
expressions are given enclosed in any non-alphameric delimiters other than
|
18 |
backslash, for example
|
19 |
|
20 |
/(a|bc)x+yz/
|
21 |
|
22 |
White space before the initial delimiter is ignored. A regular expression may
|
23 |
be continued over several input lines, in which case the newline characters are
|
24 |
included within it. See the test input files in the testdata directory for many
|
25 |
examples. It is possible to include the delimiter within the pattern by
|
26 |
escaping it, for example
|
27 |
|
28 |
/abc\/def/
|
29 |
|
30 |
If you do so, the escape and the delimiter form part of the pattern, but since
|
31 |
delimiters are always non-alphameric, this does not affect its interpretation.
|
32 |
If the terminating delimiter is immediately followed by a backslash, for
|
33 |
example,
|
34 |
|
35 |
/abc/\
|
36 |
|
37 |
then a backslash is added to the end of the pattern. This is done to provide a
|
38 |
way of testing the error condition that arises if a pattern finishes with a
|
39 |
backslash, because
|
40 |
|
41 |
/abc\/
|
42 |
|
43 |
is interpreted as the first line of a pattern that starts with "abc/", causing
|
44 |
pcretest to read the next line as a continuation of the regular expression.
|
45 |
|
46 |
|
47 |
PATTERN MODIFIERS
|
48 |
-----------------
|
49 |
|
50 |
The pattern may be followed by i, m, s, or x to set the PCRE_CASELESS,
|
51 |
PCRE_MULTILINE, PCRE_DOTALL, or PCRE_EXTENDED options, respectively. For
|
52 |
example:
|
53 |
|
54 |
/caseless/i
|
55 |
|
56 |
These modifier letters have the same effect as they do in Perl. There are
|
57 |
others which set PCRE options that do not correspond to anything in Perl: /A,
|
58 |
/E, and /X set PCRE_ANCHORED, PCRE_DOLLAR_ENDONLY, and PCRE_EXTRA respectively.
|
59 |
|
60 |
Searching for all possible matches within each subject string can be requested
|
61 |
by the /g or /G modifier. After finding a match, PCRE is called again to search
|
62 |
the remainder of the subject string. The difference between /g and /G is that
|
63 |
the former uses the startoffset argument to pcre_exec() to start searching at
|
64 |
a new point within the entire string (which is in effect what Perl does),
|
65 |
whereas the latter passes over a shortened substring. This makes a difference
|
66 |
to the matching process if the pattern begins with a lookbehind assertion
|
67 |
(including \b or \B).
|
68 |
|
69 |
If any call to pcre_exec() in a /g or /G sequence matches an empty string, the
|
70 |
next call is done with the PCRE_NOTEMPTY and PCRE_ANCHORED flags set in order
|
71 |
to search for another, non-empty, match at the same point. If this second match
|
72 |
fails, the start offset is advanced by one, and the normal match is retried.
|
73 |
This imitates the way Perl handles such cases when using the /g modifier or the
|
74 |
split() function.
|
75 |
|
76 |
There are a number of other modifiers for controlling the way pcretest
|
77 |
operates.
|
78 |
|
79 |
The /+ modifier requests that as well as outputting the substring that matched
|
80 |
the entire pattern, pcretest should in addition output the remainder of the
|
81 |
subject string. This is useful for tests where the subject contains multiple
|
82 |
copies of the same substring.
|
83 |
|
84 |
The /L modifier must be followed directly by the name of a locale, for example,
|
85 |
|
86 |
/pattern/Lfr
|
87 |
|
88 |
For this reason, it must be the last modifier letter. The given locale is set,
|
89 |
pcre_maketables() is called to build a set of character tables for the locale,
|
90 |
and this is then passed to pcre_compile() when compiling the regular
|
91 |
expression. Without an /L modifier, NULL is passed as the tables pointer; that
|
92 |
is, /L applies only to the expression on which it appears.
|
93 |
|
94 |
The /I modifier requests that pcretest output information about the compiled
|
95 |
expression (whether it is anchored, has a fixed first character, and so on). It
|
96 |
does this by calling pcre_fullinfo() after compiling an expression, and
|
97 |
outputting the information it gets back. If the pattern is studied, the results
|
98 |
of that are also output.
|
99 |
|
100 |
The /D modifier is a PCRE debugging feature, which also assumes /I. It causes
|
101 |
the internal form of compiled regular expressions to be output after
|
102 |
compilation.
|
103 |
|
104 |
The /S modifier causes pcre_study() to be called after the expression has been
|
105 |
compiled, and the results used when the expression is matched.
|
106 |
|
107 |
The /M modifier causes the size of memory block used to hold the compiled
|
108 |
pattern to be output.
|
109 |
|
110 |
The /P modifier causes pcretest to call PCRE via the POSIX wrapper API rather
|
111 |
than its native API. When this is done, all other modifiers except /i, /m, and
|
112 |
/+ are ignored. REG_ICASE is set if /i is present, and REG_NEWLINE is set if /m
|
113 |
is present. The wrapper functions force PCRE_DOLLAR_ENDONLY always, and
|
114 |
PCRE_DOTALL unless REG_NEWLINE is set.
|
115 |
|
116 |
The /8 modifier causes pcretest to call PCRE with the PCRE_UTF8 option set.
|
117 |
This turns on the (currently incomplete) support for UTF-8 character handling
|
118 |
in PCRE, provided that it was compiled with this support enabled. This modifier
|
119 |
also causes any non-printing characters in output strings to be printed using
|
120 |
the \x{hh...} notation if they are valid UTF-8 sequences.
|
121 |
|
122 |
|
123 |
DATA LINES
|
124 |
----------
|
125 |
|
126 |
Before each data line is passed to pcre_exec(), leading and trailing whitespace
|
127 |
is removed, and it is then scanned for \ escapes. The following are recognized:
|
128 |
|
129 |
\a alarm (= BEL)
|
130 |
\b backspace
|
131 |
\e escape
|
132 |
\f formfeed
|
133 |
\n newline
|
134 |
\r carriage return
|
135 |
\t tab
|
136 |
\v vertical tab
|
137 |
\nnn octal character (up to 3 octal digits)
|
138 |
\xhh hexadecimal character (up to 2 hex digits)
|
139 |
\x{hh...} hexadecimal UTF-8 character
|
140 |
|
141 |
\A pass the PCRE_ANCHORED option to pcre_exec()
|
142 |
\B pass the PCRE_NOTBOL option to pcre_exec()
|
143 |
\Cdd call pcre_copy_substring() for substring dd after a successful
|
144 |
match (any decimal number less than 32)
|
145 |
\Gdd call pcre_get_substring() for substring dd after a successful
|
146 |
match (any decimal number less than 32)
|
147 |
\L call pcre_get_substringlist() after a successful match
|
148 |
\N pass the PCRE_NOTEMPTY option to pcre_exec()
|
149 |
\Odd set the size of the output vector passed to pcre_exec() to dd
|
150 |
(any number of decimal digits)
|
151 |
\Z pass the PCRE_NOTEOL option to pcre_exec()
|
152 |
|
153 |
A backslash followed by anything else just escapes the anything else. If the
|
154 |
very last character is a backslash, it is ignored. This gives a way of passing
|
155 |
an empty line as data, since a real empty line terminates the data input.
|
156 |
|
157 |
If /P was present on the regex, causing the POSIX wrapper API to be used, only
|
158 |
\B, and \Z have any effect, causing REG_NOTBOL and REG_NOTEOL to be passed to
|
159 |
regexec() respectively.
|
160 |
|
161 |
The use of \x{hh...} to represent UTF-8 characters is not dependent on the use
|
162 |
of the /8 modifier on the pattern. It is recognized always. There may be any
|
163 |
number of hexadecimal digits inside the braces. The result is from one to six
|
164 |
bytes, encoded according to the UTF-8 rules.
|
165 |
|
166 |
|
167 |
OUTPUT FROM PCRETEST
|
168 |
--------------------
|
169 |
|
170 |
When a match succeeds, pcretest outputs the list of captured substrings that
|
171 |
pcre_exec() returns, starting with number 0 for the string that matched the
|
172 |
whole pattern. Here is an example of an interactive pcretest run.
|
173 |
|
174 |
$ pcretest
|
175 |
PCRE version 2.06 08-Jun-1999
|
176 |
|
177 |
re> /^abc(\d+)/
|
178 |
data> abc123
|
179 |
0: abc123
|
180 |
1: 123
|
181 |
data> xyz
|
182 |
No match
|
183 |
|
184 |
If the strings contain any non-printing characters, they are output as \0x
|
185 |
escapes, or as \x{...} escapes if the /8 modifier was present on the pattern.
|
186 |
If the pattern has the /+ modifier, then the output for substring 0 is followed
|
187 |
by the the rest of the subject string, identified by "0+" like this:
|
188 |
|
189 |
re> /cat/+
|
190 |
data> cataract
|
191 |
0: cat
|
192 |
0+ aract
|
193 |
|
194 |
If the pattern has the /g or /G modifier, the results of successive matching
|
195 |
attempts are output in sequence, like this:
|
196 |
|
197 |
re> /\Bi(\w\w)/g
|
198 |
data> Mississippi
|
199 |
0: iss
|
200 |
1: ss
|
201 |
0: iss
|
202 |
1: ss
|
203 |
0: ipp
|
204 |
1: pp
|
205 |
|
206 |
"No match" is output only if the first match attempt fails.
|
207 |
|
208 |
If any of \C, \G, or \L are present in a data line that is successfully
|
209 |
matched, the substrings extracted by the convenience functions are output with
|
210 |
C, G, or L after the string number instead of a colon. This is in addition to
|
211 |
the normal full list. The string length (that is, the return from the
|
212 |
extraction function) is given in parentheses after each string for \C and \G.
|
213 |
|
214 |
Note that while patterns can be continued over several lines (a plain ">"
|
215 |
prompt is used for continuations), data lines may not. However newlines can be
|
216 |
included in data by means of the \n escape.
|
217 |
|
218 |
|
219 |
COMMAND LINE OPTIONS
|
220 |
--------------------
|
221 |
|
222 |
If the -p option is given to pcretest, it is equivalent to adding /P to each
|
223 |
regular expression: the POSIX wrapper API is used to call PCRE. None of the
|
224 |
following flags has any effect in this case.
|
225 |
|
226 |
If the option -d is given to pcretest, it is equivalent to adding /D to each
|
227 |
regular expression: the internal form is output after compilation.
|
228 |
|
229 |
If the option -i is given to pcretest, it is equivalent to adding /I to each
|
230 |
regular expression: information about the compiled pattern is given after
|
231 |
compilation.
|
232 |
|
233 |
If the option -m is given to pcretest, it outputs the size of each compiled
|
234 |
pattern after it has been compiled. It is equivalent to adding /M to each
|
235 |
regular expression. For compatibility with earlier versions of pcretest, -s is
|
236 |
a synonym for -m.
|
237 |
|
238 |
If the -t option is given, each compile, study, and match is run 20000 times
|
239 |
while being timed, and the resulting time per compile or match is output in
|
240 |
milliseconds. Do not set -t with -m, because you will then get the size output
|
241 |
20000 times and the timing will be distorted. If you want to change the number
|
242 |
of repetitions used for timing, edit the definition of LOOPREPEAT at the top of
|
243 |
pcretest.c
|
244 |
|
245 |
Philip Hazel <ph10@cam.ac.uk>
|
246 |
August 2000
|