ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 87 - (show annotations)
Sat Feb 24 21:41:21 2007 UTC (14 years, 2 months ago) by nigel
File size: 23111 byte(s)
Error occurred while calculating annotation data.
Load pcre-6.5 into code/trunk.
3 pcretest - a program for testing Perl-compatible regular expressions.
5 .rs
6 .sp
7 .B pcretest "[-C] [-d] [-dfa] [-i] [-m] [-o osize] [-p] [-t] [source]"
8 .ti +5n
9 .B "[destination]"
10 .P
11 \fBpcretest\fP was written as a test program for the PCRE regular expression
12 library itself, but it can also be used for experimenting with regular
13 expressions. This document describes the features of the test program; for
14 details of the regular expressions themselves, see the
15 .\" HREF
16 \fBpcrepattern\fP
17 .\"
18 documentation. For details of the PCRE library function calls and their
19 options, see the
20 .\" HREF
21 \fBpcreapi\fP
22 .\"
23 documentation.
24 .
25 .
27 .rs
28 .TP 10
29 \fB-C\fP
30 Output the version number of the PCRE library, and all available information
31 about the optional features that are included, and then exit.
32 .TP 10
33 \fB-d\fP
34 Behave as if each regex has the \fB/D\fP (debug) modifier; the internal
35 form is output after compilation.
36 .TP 10
37 \fB-dfa\fP
38 Behave as if each data line contains the \eD escape sequence; this causes the
39 alternative matching function, \fBpcre_dfa_exec()\fP, to be used instead of the
40 standard \fBpcre_exec()\fP function (more detail is given below).
41 .TP 10
42 \fB-i\fP
43 Behave as if each regex has the \fB/I\fP modifier; information about the
44 compiled pattern is given after compilation.
45 .TP 10
46 \fB-m\fP
47 Output the size of each compiled pattern after it has been compiled. This is
48 equivalent to adding \fB/M\fP to each regular expression. For compatibility
49 with earlier versions of pcretest, \fB-s\fP is a synonym for \fB-m\fP.
50 .TP 10
51 \fB-o\fP \fIosize\fP
52 Set the number of elements in the output vector that is used when calling
53 \fBpcre_exec()\fP to be \fIosize\fP. The default value is 45, which is enough
54 for 14 capturing subexpressions. The vector size can be changed for individual
55 matching calls by including \eO in the data line (see below).
56 .TP 10
57 \fB-p\fP
58 Behave as if each regex has the \fB/P\fP modifier; the POSIX wrapper API is
59 used to call PCRE. None of the other options has any effect when \fB-p\fP is
60 set.
61 .TP 10
62 \fP-q\fP
63 Do not output the version number of \fBpcretest\fP at the start of execution.
64 .TP 10
65 \fB-t\fP
66 Run each compile, study, and match many times with a timer, and output
67 resulting time per compile or match (in milliseconds). Do not set \fB-m\fP with
68 \fB-t\fP, because you will then get the size output a zillion times, and the
69 timing will be distorted.
70 .
71 .
73 .rs
74 .sp
75 If \fBpcretest\fP is given two filename arguments, it reads from the first and
76 writes to the second. If it is given only one filename argument, it reads from
77 that file and writes to stdout. Otherwise, it reads from stdin and writes to
78 stdout, and prompts for each line of input, using "re>" to prompt for regular
79 expressions, and "data>" to prompt for data lines.
80 .P
81 The program handles any number of sets of input on a single input file. Each
82 set starts with a regular expression, and continues with any number of data
83 lines to be matched against the pattern.
84 .P
85 Each data line is matched separately and independently. If you want to do
86 multiple-line matches, you have to use the \en escape sequence in a single line
87 of input to encode the newline characters. The maximum length of data line is
88 30,000 characters.
89 .P
90 An empty line signals the end of the data lines, at which point a new regular
91 expression is read. The regular expressions are given enclosed in any
92 non-alphanumeric delimiters other than backslash, for example
93 .sp
94 /(a|bc)x+yz/
95 .sp
96 White space before the initial delimiter is ignored. A regular expression may
97 be continued over several input lines, in which case the newline characters are
98 included within it. It is possible to include the delimiter within the pattern
99 by escaping it, for example
100 .sp
101 /abc\e/def/
102 .sp
103 If you do so, the escape and the delimiter form part of the pattern, but since
104 delimiters are always non-alphanumeric, this does not affect its interpretation.
105 If the terminating delimiter is immediately followed by a backslash, for
106 example,
107 .sp
108 /abc/\e
109 .sp
110 then a backslash is added to the end of the pattern. This is done to provide a
111 way of testing the error condition that arises if a pattern finishes with a
112 backslash, because
113 .sp
114 /abc\e/
115 .sp
116 is interpreted as the first line of a pattern that starts with "abc/", causing
117 pcretest to read the next line as a continuation of the regular expression.
118 .
119 .
121 .rs
122 .sp
123 A pattern may be followed by any number of modifiers, which are mostly single
124 characters. Following Perl usage, these are referred to below as, for example,
125 "the \fB/i\fP modifier", even though the delimiter of the pattern need not
126 always be a slash, and no slash is used when writing modifiers. Whitespace may
127 appear between the final pattern delimiter and the first modifier, and between
128 the modifiers themselves.
129 .P
130 The \fB/i\fP, \fB/m\fP, \fB/s\fP, and \fB/x\fP modifiers set the PCRE_CASELESS,
131 PCRE_MULTILINE, PCRE_DOTALL, or PCRE_EXTENDED options, respectively, when
132 \fBpcre_compile()\fP is called. These four modifier letters have the same
133 effect as they do in Perl. For example:
134 .sp
135 /caseless/i
136 .sp
137 The following table shows additional modifiers for setting PCRE options that do
138 not correspond to anything in Perl:
139 .sp
147 .sp
148 Searching for all possible matches within each subject string can be requested
149 by the \fB/g\fP or \fB/G\fP modifier. After finding a match, PCRE is called
150 again to search the remainder of the subject string. The difference between
151 \fB/g\fP and \fB/G\fP is that the former uses the \fIstartoffset\fP argument to
152 \fBpcre_exec()\fP to start searching at a new point within the entire string
153 (which is in effect what Perl does), whereas the latter passes over a shortened
154 substring. This makes a difference to the matching process if the pattern
155 begins with a lookbehind assertion (including \eb or \eB).
156 .P
157 If any call to \fBpcre_exec()\fP in a \fB/g\fP or \fB/G\fP sequence matches an
158 empty string, the next call is done with the PCRE_NOTEMPTY and PCRE_ANCHORED
159 flags set in order to search for another, non-empty, match at the same point.
160 If this second match fails, the start offset is advanced by one, and the normal
161 match is retried. This imitates the way Perl handles such cases when using the
162 \fB/g\fP modifier or the \fBsplit()\fP function.
163 .P
164 There are yet more modifiers for controlling the way \fBpcretest\fP
165 operates.
166 .P
167 The \fB/+\fP modifier requests that as well as outputting the substring that
168 matched the entire pattern, pcretest should in addition output the remainder of
169 the subject string. This is useful for tests where the subject contains
170 multiple copies of the same substring.
171 .P
172 The \fB/L\fP modifier must be followed directly by the name of a locale, for
173 example,
174 .sp
175 /pattern/Lfr_FR
176 .sp
177 For this reason, it must be the last modifier. The given locale is set,
178 \fBpcre_maketables()\fP is called to build a set of character tables for the
179 locale, and this is then passed to \fBpcre_compile()\fP when compiling the
180 regular expression. Without an \fB/L\fP modifier, NULL is passed as the tables
181 pointer; that is, \fB/L\fP applies only to the expression on which it appears.
182 .P
183 The \fB/I\fP modifier requests that \fBpcretest\fP output information about the
184 compiled pattern (whether it is anchored, has a fixed first character, and
185 so on). It does this by calling \fBpcre_fullinfo()\fP after compiling a
186 pattern. If the pattern is studied, the results of that are also output.
187 .P
188 The \fB/D\fP modifier is a PCRE debugging feature, which also assumes \fB/I\fP.
189 It causes the internal form of compiled regular expressions to be output after
190 compilation. If the pattern was studied, the information returned is also
191 output.
192 .P
193 The \fB/F\fP modifier causes \fBpcretest\fP to flip the byte order of the
194 fields in the compiled pattern that contain 2-byte and 4-byte numbers. This
195 facility is for testing the feature in PCRE that allows it to execute patterns
196 that were compiled on a host with a different endianness. This feature is not
197 available when the POSIX interface to PCRE is being used, that is, when the
198 \fB/P\fP pattern modifier is specified. See also the section about saving and
199 reloading compiled patterns below.
200 .P
201 The \fB/S\fP modifier causes \fBpcre_study()\fP to be called after the
202 expression has been compiled, and the results used when the expression is
203 matched.
204 .P
205 The \fB/M\fP modifier causes the size of memory block used to hold the compiled
206 pattern to be output.
207 .P
208 The \fB/P\fP modifier causes \fBpcretest\fP to call PCRE via the POSIX wrapper
209 API rather than its native API. When this is done, all other modifiers except
210 \fB/i\fP, \fB/m\fP, and \fB/+\fP are ignored. REG_ICASE is set if \fB/i\fP is
211 present, and REG_NEWLINE is set if \fB/m\fP is present. The wrapper functions
212 force PCRE_DOLLAR_ENDONLY always, and PCRE_DOTALL unless REG_NEWLINE is set.
213 .P
214 The \fB/8\fP modifier causes \fBpcretest\fP to call PCRE with the PCRE_UTF8
215 option set. This turns on support for UTF-8 character handling in PCRE,
216 provided that it was compiled with this support enabled. This modifier also
217 causes any non-printing characters in output strings to be printed using the
218 \ex{hh...} notation if they are valid UTF-8 sequences.
219 .P
220 If the \fB/?\fP modifier is used with \fB/8\fP, it causes \fBpcretest\fP to
221 call \fBpcre_compile()\fP with the PCRE_NO_UTF8_CHECK option, to suppress the
222 checking of the string for UTF-8 validity.
223 .
224 .
226 .rs
227 .sp
228 Before each data line is passed to \fBpcre_exec()\fP, leading and trailing
229 whitespace is removed, and it is then scanned for \e escapes. Some of these are
230 pretty esoteric features, intended for checking out some of the more
231 complicated features of PCRE. If you are just testing "ordinary" regular
232 expressions, you probably don't need any of these. The following escapes are
233 recognized:
234 .sp
235 \ea alarm (= BEL)
236 \eb backspace
237 \ee escape
238 \ef formfeed
239 \en newline
240 \er carriage return
241 \et tab
242 \ev vertical tab
243 \ennn octal character (up to 3 octal digits)
244 \exhh hexadecimal character (up to 2 hex digits)
245 .\" JOIN
246 \ex{hh...} hexadecimal character, any number of digits
247 in UTF-8 mode
248 \eA pass the PCRE_ANCHORED option to \fBpcre_exec()\fP
249 \eB pass the PCRE_NOTBOL option to \fBpcre_exec()\fP
250 .\" JOIN
251 \eCdd call pcre_copy_substring() for substring dd
252 after a successful match (number less than 32)
253 .\" JOIN
254 \eCname call pcre_copy_named_substring() for substring
255 "name" after a successful match (name termin-
256 ated by next non alphanumeric character)
257 .\" JOIN
258 \eC+ show the current captured substrings at callout
259 time
260 \eC- do not supply a callout function
261 .\" JOIN
262 \eC!n return 1 instead of 0 when callout number n is
263 reached
264 .\" JOIN
265 \eC!n!m return 1 instead of 0 when callout number n is
266 reached for the nth time
267 .\" JOIN
268 \eC*n pass the number n (may be negative) as callout
269 data; this is used as the callout return value
270 \eD use the \fBpcre_dfa_exec()\fP match function
271 \eF only shortest match for \fBpcre_dfa_exec()\fP
272 .\" JOIN
273 \eGdd call pcre_get_substring() for substring dd
274 after a successful match (number less than 32)
275 .\" JOIN
276 \eGname call pcre_get_named_substring() for substring
277 "name" after a successful match (name termin-
278 ated by next non-alphanumeric character)
279 .\" JOIN
280 \eL call pcre_get_substringlist() after a
281 successful match
282 \eM discover the minimum MATCH_LIMIT and
284 \eN pass the PCRE_NOTEMPTY option to \fBpcre_exec()\fP
285 .\" JOIN
286 \eOdd set the size of the output vector passed to
287 \fBpcre_exec()\fP to dd (any number of digits)
288 .\" JOIN
289 \eP pass the PCRE_PARTIAL option to \fBpcre_exec()\fP
290 or \fBpcre_dfa_exec()\fP
291 \eR pass the PCRE_DFA_RESTART option to \fBpcre_dfa_exec()\fP
292 \eS output details of memory get/free calls during matching
293 \eZ pass the PCRE_NOTEOL option to \fBpcre_exec()\fP
294 .\" JOIN
295 \e? pass the PCRE_NO_UTF8_CHECK option to
296 \fBpcre_exec()\fP
297 \e>dd start the match at offset dd (any number of digits);
298 this sets the \fIstartoffset\fP argument for \fBpcre_exec()\fP
299 .sp
300 A backslash followed by anything else just escapes the anything else. If the
301 very last character is a backslash, it is ignored. This gives a way of passing
302 an empty line as data, since a real empty line terminates the data input.
303 .P
304 If \eM is present, \fBpcretest\fP calls \fBpcre_exec()\fP several times, with
305 different values in the \fImatch_limit\fP and \fImatch_limit_recursion\fP
306 fields of the \fBpcre_extra\fP data structure, until it finds the minimum
307 numbers for each parameter that allow \fBpcre_exec()\fP to complete. The
308 \fImatch_limit\fP number is a measure of the amount of backtracking that takes
309 place, and checking it out can be instructive. For most simple matches, the
310 number is quite small, but for patterns with very large numbers of matching
311 possibilities, it can become large very quickly with increasing length of
312 subject string. The \fImatch_limit_recursion\fP number is a measure of how much
313 stack (or, if PCRE is compiled with NO_RECURSE, how much heap) memory is needed
314 to complete the match attempt.
315 .P
316 When \eO is used, the value specified may be higher or lower than the size set
317 by the \fB-O\fP command line option (or defaulted to 45); \eO applies only to
318 the call of \fBpcre_exec()\fP for the line in which it appears.
319 .P
320 If the \fB/P\fP modifier was present on the pattern, causing the POSIX wrapper
321 API to be used, the only option-setting sequences that have any effect are \eB
322 and \eZ, causing REG_NOTBOL and REG_NOTEOL, respectively, to be passed to
323 \fBregexec()\fP.
324 .P
325 The use of \ex{hh...} to represent UTF-8 characters is not dependent on the use
326 of the \fB/8\fP modifier on the pattern. It is recognized always. There may be
327 any number of hexadecimal digits inside the braces. The result is from one to
328 six bytes, encoded according to the UTF-8 rules.
329 .
330 .
332 .rs
333 .sp
334 By default, \fBpcretest\fP uses the standard PCRE matching function,
335 \fBpcre_exec()\fP to match each data line. From release 6.0, PCRE supports an
336 alternative matching function, \fBpcre_dfa_test()\fP, which operates in a
337 different way, and has some restrictions. The differences between the two
338 functions are described in the
339 .\" HREF
340 \fBpcrematching\fP
341 .\"
342 documentation.
343 .P
344 If a data line contains the \eD escape sequence, or if the command line
345 contains the \fB-dfa\fP option, the alternative matching function is called.
346 This function finds all possible matches at a given point. If, however, the \eF
347 escape sequence is present in the data line, it stops after the first match is
348 found. This is always the shortest possible match.
349 .
350 .
352 .rs
353 .sp
354 This section describes the output when the normal matching function,
355 \fBpcre_exec()\fP, is being used.
356 .P
357 When a match succeeds, pcretest outputs the list of captured substrings that
358 \fBpcre_exec()\fP returns, starting with number 0 for the string that matched
359 the whole pattern. Otherwise, it outputs "No match" or "Partial match"
360 when \fBpcre_exec()\fP returns PCRE_ERROR_NOMATCH or PCRE_ERROR_PARTIAL,
361 respectively, and otherwise the PCRE negative error number. Here is an example
362 of an interactive \fBpcretest\fP run.
363 .sp
364 $ pcretest
365 PCRE version 5.00 07-Sep-2004
366 .sp
367 re> /^abc(\ed+)/
368 data> abc123
369 0: abc123
370 1: 123
371 data> xyz
372 No match
373 .sp
374 If the strings contain any non-printing characters, they are output as \e0x
375 escapes, or as \ex{...} escapes if the \fB/8\fP modifier was present on the
376 pattern. If the pattern has the \fB/+\fP modifier, the output for substring 0
377 is followed by the the rest of the subject string, identified by "0+" like
378 this:
379 .sp
380 re> /cat/+
381 data> cataract
382 0: cat
383 0+ aract
384 .sp
385 If the pattern has the \fB/g\fP or \fB/G\fP modifier, the results of successive
386 matching attempts are output in sequence, like this:
387 .sp
388 re> /\eBi(\ew\ew)/g
389 data> Mississippi
390 0: iss
391 1: ss
392 0: iss
393 1: ss
394 0: ipp
395 1: pp
396 .sp
397 "No match" is output only if the first match attempt fails.
398 .P
399 If any of the sequences \fB\eC\fP, \fB\eG\fP, or \fB\eL\fP are present in a
400 data line that is successfully matched, the substrings extracted by the
401 convenience functions are output with C, G, or L after the string number
402 instead of a colon. This is in addition to the normal full list. The string
403 length (that is, the return from the extraction function) is given in
404 parentheses after each string for \fB\eC\fP and \fB\eG\fP.
405 .P
406 Note that while patterns can be continued over several lines (a plain ">"
407 prompt is used for continuations), data lines may not. However newlines can be
408 included in data by means of the \en escape.
409 .
410 .
412 .rs
413 .sp
414 When the alternative matching function, \fBpcre_dfa_exec()\fP, is used (by
415 means of the \eD escape sequence or the \fB-dfa\fP command line option), the
416 output consists of a list of all the matches that start at the first point in
417 the subject where there is at least one match. For example:
418 .sp
419 re> /(tang|tangerine|tan)/
420 data> yellow tangerine\eD
421 0: tangerine
422 1: tang
423 2: tan
424 .sp
425 (Using the normal matching function on this data finds only "tang".) The
426 longest matching string is always given first (and numbered zero).
427 .P
428 If \fB/g\P is present on the pattern, the search for further matches resumes
429 at the end of the longest match. For example:
430 .sp
431 re> /(tang|tangerine|tan)/g
432 data> yellow tangerine and tangy sultana\eD
433 0: tangerine
434 1: tang
435 2: tan
436 0: tang
437 1: tan
438 0: tan
439 .sp
440 Since the matching function does not support substring capture, the escape
441 sequences that are concerned with captured substrings are not relevant.
442 .
443 .
445 .rs
446 .sp
447 When the alternative matching function has given the PCRE_ERROR_PARTIAL return,
448 indicating that the subject partially matched the pattern, you can restart the
449 match with additional subject data by means of the \eR escape sequence. For
450 example:
451 .sp
452 re> /^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$/
453 data> 23ja\eP\eD
454 Partial match: 23ja
455 data> n05\eR\eD
456 0: n05
457 .sp
458 For further information about partial matching, see the
459 .\" HREF
460 \fBpcrepartial\fP
461 .\"
462 documentation.
463 .
464 .
466 .rs
467 .sp
468 If the pattern contains any callout requests, \fBpcretest\fP's callout function
469 is called during matching. This works with both matching functions. By default,
470 the called function displays the callout number, the start and current
471 positions in the text at the callout time, and the next pattern item to be
472 tested. For example, the output
473 .sp
474 --->pqrabcdef
475 0 ^ ^ \ed
476 .sp
477 indicates that callout number 0 occurred for a match attempt starting at the
478 fourth character of the subject string, when the pointer was at the seventh
479 character of the data, and when the next pattern item was \ed. Just one
480 circumflex is output if the start and current positions are the same.
481 .P
482 Callouts numbered 255 are assumed to be automatic callouts, inserted as a
483 result of the \fB/C\fP pattern modifier. In this case, instead of showing the
484 callout number, the offset in the pattern, preceded by a plus, is output. For
485 example:
486 .sp
487 re> /\ed?[A-E]\e*/C
488 data> E*
489 --->E*
490 +0 ^ \ed?
491 +3 ^ [A-E]
492 +8 ^^ \e*
493 +10 ^ ^
494 0: E*
495 .sp
496 The callout function in \fBpcretest\fP returns zero (carry on matching) by
497 default, but you can use a \eC item in a data line (as described above) to
498 change this.
499 .P
500 Inserting callouts can be helpful when using \fBpcretest\fP to check
501 complicated regular expressions. For further information about callouts, see
502 the
503 .\" HREF
504 \fBpcrecallout\fP
505 .\"
506 documentation.
507 .
508 .
510 .rs
511 .sp
512 The facilities described in this section are not available when the POSIX
513 inteface to PCRE is being used, that is, when the \fB/P\fP pattern modifier is
514 specified.
515 .P
516 When the POSIX interface is not in use, you can cause \fBpcretest\fP to write a
517 compiled pattern to a file, by following the modifiers with > and a file name.
518 For example:
519 .sp
520 /pattern/im >/some/file
521 .sp
522 See the
523 .\" HREF
524 \fBpcreprecompile\fP
525 .\"
526 documentation for a discussion about saving and re-using compiled patterns.
527 .P
528 The data that is written is binary. The first eight bytes are the length of the
529 compiled pattern data followed by the length of the optional study data, each
530 written as four bytes in big-endian order (most significant byte first). If
531 there is no study data (either the pattern was not studied, or studying did not
532 return any data), the second length is zero. The lengths are followed by an
533 exact copy of the compiled pattern. If there is additional study data, this
534 follows immediately after the compiled pattern. After writing the file,
535 \fBpcretest\fP expects to read a new pattern.
536 .P
537 A saved pattern can be reloaded into \fBpcretest\fP by specifing < and a file
538 name instead of a pattern. The name of the file must not contain a < character,
539 as otherwise \fBpcretest\fP will interpret the line as a pattern delimited by <
540 characters.
541 For example:
542 .sp
543 re> </some/file
544 Compiled regex loaded from /some/file
545 No study data
546 .sp
547 When the pattern has been loaded, \fBpcretest\fP proceeds to read data lines in
548 the usual way.
549 .P
550 You can copy a file written by \fBpcretest\fP to a different host and reload it
551 there, even if the new host has opposite endianness to the one on which the
552 pattern was compiled. For example, you can compile on an i86 machine and run on
553 a SPARC machine.
554 .P
555 File names for saving and reloading can be absolute or relative, but note that
556 the shell facility of expanding a file name that starts with a tilde (~) is not
557 available.
558 .P
559 The ability to save and reload files in \fBpcretest\fP is intended for testing
560 and experimentation. It is not intended for production use because only a
561 single pattern can be written to a file. Furthermore, there is no facility for
562 supplying custom character tables for use with a reloaded pattern. If the
563 original pattern was compiled with custom tables, an attempt to match a subject
564 string using a reloaded pattern is likely to cause \fBpcretest\fP to crash.
565 Finally, if you attempt to load a file that is not in the correct format, the
566 result is undefined.
567 .
568 .
570 .rs
571 .sp
572 Philip Hazel
573 .br
574 University Computing Service,
575 .br
576 Cambridge CB2 3QG, England.
577 .P
578 .in 0
579 Last updated: 18 January 2006
580 .br
581 Copyright (c) 1997-2006 University of Cambridge.

  ViewVC Help
Powered by ViewVC 1.1.5