ViewVC logotype

Contents of /code/trunk/doc/html/pcretest.html

Parent Directory Parent Directory | Revision Log Revision Log

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

  ViewVC Help
Powered by ViewVC 1.1.5