/[pcre]/code/trunk/doc/pcretest.1
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 73 - (hide annotations) (download)
Sat Feb 24 21:40:30 2007 UTC (7 years, 9 months ago) by nigel
File size: 15127 byte(s)
Load pcre-4.5 into code/trunk.

1 nigel 53 .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 nigel 63 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 nigel 53
20     .SH OPTIONS
21 nigel 63 .rs
22     .sp
23 nigel 53 .TP 10
24 nigel 63 \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 nigel 53 \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 nigel 63 Run each compile, study, and match many times with a timer, and output
53 nigel 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 nigel 63 .rs
59     .sp
60 nigel 53 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 nigel 63 lines to be matched against the pattern.
69 nigel 53
70 nigel 63 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 nigel 53 /(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 nigel 63 .rs
106     .sp
107 nigel 53 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 nigel 71 others that set PCRE options that do not correspond to anything in Perl:
115     \fB/A\fR, \fB/E\fR, \fB/N\fR, \fB/U\fR, and \fB/X\fR set PCRE_ANCHORED,
116     PCRE_DOLLAR_ENDONLY, PCRE_NO_AUTO_CAPTURE, PCRE_UNGREEDY, and PCRE_EXTRA
117     respectively.
118 nigel 53
119     Searching for all possible matches within each subject string can be requested
120     by the \fB/g\fR or \fB/G\fR modifier. After finding a match, PCRE is called
121     again to search the remainder of the subject string. The difference between
122     \fB/g\fR and \fB/G\fR is that the former uses the \fIstartoffset\fR argument to
123     \fBpcre_exec()\fR to start searching at a new point within the entire string
124     (which is in effect what Perl does), whereas the latter passes over a shortened
125     substring. This makes a difference to the matching process if the pattern
126     begins with a lookbehind assertion (including \\b or \\B).
127    
128     If any call to \fBpcre_exec()\fR in a \fB/g\fR or \fB/G\fR sequence matches an
129     empty string, the next call is done with the PCRE_NOTEMPTY and PCRE_ANCHORED
130     flags set in order to search for another, non-empty, match at the same point.
131     If this second match fails, the start offset is advanced by one, and the normal
132     match is retried. This imitates the way Perl handles such cases when using the
133     \fB/g\fR modifier or the \fBsplit()\fR function.
134    
135     There are a number of other modifiers for controlling the way \fBpcretest\fR
136     operates.
137    
138     The \fB/+\fR modifier requests that as well as outputting the substring that
139     matched the entire pattern, pcretest should in addition output the remainder of
140     the subject string. This is useful for tests where the subject contains
141     multiple copies of the same substring.
142    
143     The \fB/L\fR modifier must be followed directly by the name of a locale, for
144     example,
145    
146     /pattern/Lfr
147    
148     For this reason, it must be the last modifier letter. The given locale is set,
149     \fBpcre_maketables()\fR is called to build a set of character tables for the
150     locale, and this is then passed to \fBpcre_compile()\fR when compiling the
151     regular expression. Without an \fB/L\fR modifier, NULL is passed as the tables
152     pointer; that is, \fB/L\fR applies only to the expression on which it appears.
153    
154     The \fB/I\fR modifier requests that \fBpcretest\fR output information about the
155     compiled expression (whether it is anchored, has a fixed first character, and
156     so on). It does this by calling \fBpcre_fullinfo()\fR after compiling an
157     expression, and outputting the information it gets back. If the pattern is
158     studied, the results of that are also output.
159    
160     The \fB/D\fR modifier is a PCRE debugging feature, which also assumes \fB/I\fR.
161     It causes the internal form of compiled regular expressions to be output after
162 nigel 63 compilation. If the pattern was studied, the information returned is also
163     output.
164 nigel 53
165     The \fB/S\fR modifier causes \fBpcre_study()\fR to be called after the
166     expression has been compiled, and the results used when the expression is
167     matched.
168    
169     The \fB/M\fR modifier causes the size of memory block used to hold the compiled
170     pattern to be output.
171    
172     The \fB/P\fR modifier causes \fBpcretest\fR to call PCRE via the POSIX wrapper
173     API rather than its native API. When this is done, all other modifiers except
174     \fB/i\fR, \fB/m\fR, and \fB/+\fR are ignored. REG_ICASE is set if \fB/i\fR is
175     present, and REG_NEWLINE is set if \fB/m\fR is present. The wrapper functions
176     force PCRE_DOLLAR_ENDONLY always, and PCRE_DOTALL unless REG_NEWLINE is set.
177    
178     The \fB/8\fR modifier causes \fBpcretest\fR to call PCRE with the PCRE_UTF8
179 nigel 63 option set. This turns on support for UTF-8 character handling in PCRE,
180     provided that it was compiled with this support enabled. This modifier also
181     causes any non-printing characters in output strings to be printed using the
182     \\x{hh...} notation if they are valid UTF-8 sequences.
183 nigel 53
184 nigel 71 If the \fB/?\fR modifier is used with \fB/8\fR, it causes \fBpcretest\fR to
185     call \fBpcre_compile()\fR with the PCRE_NO_UTF8_CHECK option, to suppress the
186     checking of the string for UTF-8 validity.
187    
188 nigel 63 .SH CALLOUTS
189     .rs
190     .sp
191     If the pattern contains any callout requests, \fBpcretest\fR's callout function
192     will be called. By default, it displays the callout number, and the start and
193     current positions in the text at the callout time. For example, the output
194 nigel 53
195 nigel 63 --->pqrabcdef
196     0 ^ ^
197    
198     indicates that callout number 0 occurred for a match attempt starting at the
199     fourth character of the subject string, when the pointer was at the seventh
200     character. The callout function returns zero (carry on matching) by default.
201    
202     Inserting callouts may be helpful when using \fBpcretest\fR to check
203     complicated regular expressions. For further information about callouts, see
204     the
205     .\" HREF
206     \fBpcrecallout\fR
207     .\"
208     documentation.
209    
210     For testing the PCRE library, additional control of callout behaviour is
211     available via escape sequences in the data, as described in the following
212     section. In particular, it is possible to pass in a number as callout data (the
213     default is zero). If the callout function receives a non-zero number, it
214     returns that value instead of zero.
215    
216 nigel 53 .SH DATA LINES
217 nigel 63 .rs
218     .sp
219 nigel 53 Before each data line is passed to \fBpcre_exec()\fR, leading and trailing
220 nigel 63 whitespace is removed, and it is then scanned for \\ escapes. Some of these are
221     pretty esoteric features, intended for checking out some of the more
222     complicated features of PCRE. If you are just testing "ordinary" regular
223     expressions, you probably don't need any of these. The following escapes are
224 nigel 53 recognized:
225    
226     \\a alarm (= BEL)
227     \\b backspace
228     \\e escape
229     \\f formfeed
230     \\n newline
231     \\r carriage return
232     \\t tab
233     \\v vertical tab
234     \\nnn octal character (up to 3 octal digits)
235     \\xhh hexadecimal character (up to 2 hex digits)
236 nigel 63 \\x{hh...} hexadecimal character, any number of digits
237     in UTF-8 mode
238 nigel 53 \\A pass the PCRE_ANCHORED option to \fBpcre_exec()\fR
239     \\B pass the PCRE_NOTBOL option to \fBpcre_exec()\fR
240     \\Cdd call pcre_copy_substring() for substring dd
241 nigel 63 after a successful match (any decimal number
242     less than 32)
243     \\Cname call pcre_copy_named_substring() for substring
244     "name" after a successful match (name termin-
245     ated by next non alphanumeric character)
246     \\C+ show the current captured substrings at callout
247     time
248     \\C- do not supply a callout function
249     \\C!n return 1 instead of 0 when callout number n is
250     reached
251     \\C!n!m return 1 instead of 0 when callout number n is
252     reached for the nth time
253     \\C*n pass the number n (may be negative) as callout
254     data
255 nigel 53 \\Gdd call pcre_get_substring() for substring dd
256 nigel 63 after a successful match (any decimal number
257     less than 32)
258     \\Gname call pcre_get_named_substring() for substring
259     "name" after a successful match (name termin-
260     ated by next non-alphanumeric character)
261 nigel 53 \\L call pcre_get_substringlist() after a
262 nigel 63 successful match
263     \\M discover the minimum MATCH_LIMIT setting
264 nigel 53 \\N pass the PCRE_NOTEMPTY option to \fBpcre_exec()\fR
265     \\Odd set the size of the output vector passed to
266 nigel 63 \fBpcre_exec()\fR to dd (any number of decimal
267     digits)
268 nigel 73 \\S output details of memory get/free calls during matching
269 nigel 53 \\Z pass the PCRE_NOTEOL option to \fBpcre_exec()\fR
270 nigel 71 \\? pass the PCRE_NO_UTF8_CHECK option to
271     \fBpcre_exec()\fR
272 nigel 53
273 nigel 63 If \\M is present, \fBpcretest\fR calls \fBpcre_exec()\fR several times, with
274     different values in the \fImatch_limit\fR field of the \fBpcre_extra\fR data
275     structure, until it finds the minimum number that is needed for
276     \fBpcre_exec()\fR to complete. This number is a measure of the amount of
277     recursion and backtracking that takes place, and checking it out can be
278     instructive. For most simple matches, the number is quite small, but for
279     patterns with very large numbers of matching possibilities, it can become large
280     very quickly with increasing length of subject string.
281    
282 nigel 53 When \\O is used, it may be higher or lower than the size set by the \fB-O\fR
283     option (or defaulted to 45); \\O applies only to the call of \fBpcre_exec()\fR
284     for the line in which it appears.
285    
286     A backslash followed by anything else just escapes the anything else. If the
287     very last character is a backslash, it is ignored. This gives a way of passing
288     an empty line as data, since a real empty line terminates the data input.
289    
290     If \fB/P\fR was present on the regex, causing the POSIX wrapper API to be used,
291     only \fB\B\fR, and \fB\Z\fR have any effect, causing REG_NOTBOL and REG_NOTEOL
292     to be passed to \fBregexec()\fR respectively.
293    
294     The use of \\x{hh...} to represent UTF-8 characters is not dependent on the use
295     of the \fB/8\fR modifier on the pattern. It is recognized always. There may be
296     any number of hexadecimal digits inside the braces. The result is from one to
297     six bytes, encoded according to the UTF-8 rules.
298    
299     .SH OUTPUT FROM PCRETEST
300 nigel 63 .rs
301     .sp
302 nigel 53 When a match succeeds, pcretest outputs the list of captured substrings that
303     \fBpcre_exec()\fR returns, starting with number 0 for the string that matched
304     the whole pattern. Here is an example of an interactive pcretest run.
305    
306     $ pcretest
307 nigel 63 PCRE version 4.00 08-Jan-2003
308 nigel 53
309     re> /^abc(\\d+)/
310     data> abc123
311     0: abc123
312     1: 123
313     data> xyz
314     No match
315    
316     If the strings contain any non-printing characters, they are output as \\0x
317     escapes, or as \\x{...} escapes if the \fB/8\fR modifier was present on the
318     pattern. If the pattern has the \fB/+\fR modifier, then the output for
319     substring 0 is followed by the the rest of the subject string, identified by
320     "0+" like this:
321    
322     re> /cat/+
323     data> cataract
324     0: cat
325     0+ aract
326    
327     If the pattern has the \fB/g\fR or \fB/G\fR modifier, the results of successive
328     matching attempts are output in sequence, like this:
329    
330     re> /\\Bi(\\w\\w)/g
331     data> Mississippi
332     0: iss
333     1: ss
334     0: iss
335     1: ss
336     0: ipp
337     1: pp
338    
339     "No match" is output only if the first match attempt fails.
340    
341     If any of the sequences \fB\\C\fR, \fB\\G\fR, or \fB\\L\fR are present in a
342     data line that is successfully matched, the substrings extracted by the
343     convenience functions are output with C, G, or L after the string number
344     instead of a colon. This is in addition to the normal full list. The string
345     length (that is, the return from the extraction function) is given in
346     parentheses after each string for \fB\\C\fR and \fB\\G\fR.
347    
348     Note that while patterns can be continued over several lines (a plain ">"
349     prompt is used for continuations), data lines may not. However newlines can be
350     included in data by means of the \\n escape.
351    
352     .SH AUTHOR
353 nigel 63 .rs
354     .sp
355 nigel 53 Philip Hazel <ph10@cam.ac.uk>
356     .br
357     University Computing Service,
358     .br
359     Cambridge CB2 3QG, England.
360    
361 nigel 63 .in 0
362 nigel 73 Last updated: 09 December 2003
363 nigel 53 .br
364 nigel 63 Copyright (c) 1997-2003 University of Cambridge.

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12