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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 53 - (show annotations) (download)
Sat Feb 24 21:39:42 2007 UTC (7 years, 6 months ago) by nigel
File size: 11479 byte(s)
Load pcre-3.5 into code/trunk.

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12