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