| 1 |
nigel |
3 |
README file for PCRE (Perl-compatible regular expressions) |
| 2 |
|
|
---------------------------------------------------------- |
| 3 |
|
|
|
| 4 |
nigel |
23 |
******************************************************************************* |
| 5 |
|
|
* IMPORTANT FOR THOSE UPGRADING FROM VERSIONS BEFORE 2.00 * |
| 6 |
|
|
* * |
| 7 |
|
|
* Please note that there has been a change in the API such that a larger * |
| 8 |
|
|
* ovector is required at matching time, to provide some additional workspace. * |
| 9 |
|
|
* The new man page has details. This change was necessary in order to support * |
| 10 |
|
|
* some of the new functionality in Perl 5.005. * |
| 11 |
nigel |
25 |
* * |
| 12 |
|
|
* IMPORTANT FOR THOSE UPGRADING FROM VERSION 2.00 * |
| 13 |
|
|
* * |
| 14 |
|
|
* Another (I hope this is the last!) change has been made to the API for the * |
| 15 |
|
|
* pcre_compile() function. An additional argument has been added to make it * |
| 16 |
|
|
* possible to pass over a pointer to character tables built in the current * |
| 17 |
|
|
* locale by pcre_maketables(). To use the default tables, this new arguement * |
| 18 |
|
|
* should be passed as NULL. * |
| 19 |
nigel |
23 |
******************************************************************************* |
| 20 |
|
|
|
| 21 |
nigel |
3 |
The distribution should contain the following files: |
| 22 |
|
|
|
| 23 |
|
|
ChangeLog log of changes to the code |
| 24 |
nigel |
29 |
LICENCE conditions for the use of PCRE |
| 25 |
nigel |
3 |
Makefile for building PCRE |
| 26 |
|
|
README this file |
| 27 |
nigel |
23 |
RunTest a shell script for running tests |
| 28 |
nigel |
3 |
Tech.Notes notes on the encoding |
| 29 |
|
|
pcre.3 man page for the functions |
| 30 |
|
|
pcreposix.3 man page for the POSIX wrapper API |
| 31 |
nigel |
27 |
dftables.c auxiliary program for building chartables.c |
| 32 |
nigel |
29 |
get.c ) |
| 33 |
nigel |
25 |
maketables.c ) |
| 34 |
nigel |
3 |
study.c ) source of |
| 35 |
|
|
pcre.c ) the functions |
| 36 |
|
|
pcreposix.c ) |
| 37 |
|
|
pcre.h header for the external API |
| 38 |
|
|
pcreposix.h header for the external POSIX wrapper API |
| 39 |
|
|
internal.h header for internal use |
| 40 |
|
|
pcretest.c test program |
| 41 |
|
|
pgrep.1 man page for pgrep |
| 42 |
|
|
pgrep.c source of a grep utility that uses PCRE |
| 43 |
|
|
perltest Perl test program |
| 44 |
nigel |
23 |
testinput test data, compatible with Perl 5.004 and 5.005 |
| 45 |
nigel |
3 |
testinput2 test data for error messages and non-Perl things |
| 46 |
nigel |
23 |
testinput3 test data, compatible with Perl 5.005 |
| 47 |
nigel |
25 |
testinput4 test data for locale-specific tests |
| 48 |
nigel |
3 |
testoutput test results corresponding to testinput |
| 49 |
|
|
testoutput2 test results corresponding to testinput2 |
| 50 |
nigel |
25 |
testoutput3 test results corresponding to testinput3 |
| 51 |
|
|
testoutput4 test results corresponding to testinput4 |
| 52 |
nigel |
3 |
|
| 53 |
nigel |
23 |
To build PCRE, edit Makefile for your system (it is a fairly simple make file, |
| 54 |
|
|
and there are some comments at the top) and then run it. It builds two |
| 55 |
|
|
libraries called libpcre.a and libpcreposix.a, a test program called pcretest, |
| 56 |
|
|
and the pgrep command. |
| 57 |
nigel |
3 |
|
| 58 |
nigel |
23 |
To test PCRE, run the RunTest script in the pcre directory. This runs pcretest |
| 59 |
|
|
on each of the testinput files in turn, and compares the output with the |
| 60 |
|
|
contents of the corresponding testoutput file. A file called testtry is used to |
| 61 |
|
|
hold the output from pcretest (which is documented below). |
| 62 |
nigel |
3 |
|
| 63 |
nigel |
23 |
To run pcretest on just one of the test files, give its number as an argument |
| 64 |
|
|
to RunTest, for example: |
| 65 |
nigel |
3 |
|
| 66 |
nigel |
23 |
RunTest 3 |
| 67 |
nigel |
3 |
|
| 68 |
nigel |
23 |
The first and third test files can also be fed directly into the perltest |
| 69 |
|
|
program to check that Perl gives the same results. The third file requires the |
| 70 |
|
|
additional features of release 5.005, which is why it is kept separate from the |
| 71 |
|
|
main test input, which needs only Perl 5.004. In the long run, when 5.005 is |
| 72 |
|
|
widespread, these two test files may get amalgamated. |
| 73 |
nigel |
3 |
|
| 74 |
nigel |
29 |
The second set of tests check pcre_info(), pcre_study(), pcre_copy_substring(), |
| 75 |
|
|
pcre_get_substring(), pcre_get_substring_list(), error detection and run-time |
| 76 |
|
|
flags that are specific to PCRE, as well as the POSIX wrapper API. |
| 77 |
nigel |
7 |
|
| 78 |
nigel |
25 |
The fourth set of tests checks pcre_maketables(), the facility for building a |
| 79 |
|
|
set of character tables for a specific locale and using them instead of the |
| 80 |
|
|
default tables. The tests make use of the "fr" (French) locale. Before running |
| 81 |
|
|
the test, the script checks for the presence of this locale by running the |
| 82 |
|
|
"locale" command. If that command fails, or if it doesn't include "fr" in the |
| 83 |
|
|
list of available locales, the fourth test cannot be run, and a comment is |
| 84 |
|
|
output to say why. If running this test produces instances of the error |
| 85 |
|
|
|
| 86 |
|
|
** Failed to set locale "fr" |
| 87 |
|
|
|
| 88 |
|
|
in the comparison output, it means that locale is not available on your system, |
| 89 |
|
|
despite being listed by "locale". This does not mean that PCRE is broken. |
| 90 |
|
|
|
| 91 |
nigel |
3 |
To install PCRE, copy libpcre.a to any suitable library directory (e.g. |
| 92 |
|
|
/usr/local/lib), pcre.h to any suitable include directory (e.g. |
| 93 |
|
|
/usr/local/include), and pcre.3 to any suitable man directory (e.g. |
| 94 |
|
|
/usr/local/man/man3). |
| 95 |
|
|
|
| 96 |
|
|
To install the pgrep command, copy it to any suitable binary directory, (e.g. |
| 97 |
|
|
/usr/local/bin) and pgrep.1 to any suitable man directory (e.g. |
| 98 |
|
|
/usr/local/man/man1). |
| 99 |
|
|
|
| 100 |
|
|
PCRE has its own native API, but a set of "wrapper" functions that are based on |
| 101 |
|
|
the POSIX API are also supplied in the library libpcreposix.a. Note that this |
| 102 |
|
|
just provides a POSIX calling interface to PCRE: the regular expressions |
| 103 |
|
|
themselves still follow Perl syntax and semantics. The header file |
| 104 |
|
|
for the POSIX-style functions is called pcreposix.h. The official POSIX name is |
| 105 |
|
|
regex.h, but I didn't want to risk possible problems with existing files of |
| 106 |
|
|
that name by distributing it that way. To use it with an existing program that |
| 107 |
nigel |
23 |
uses the POSIX API, it will have to be renamed or pointed at by a link. |
| 108 |
nigel |
3 |
|
| 109 |
|
|
|
| 110 |
|
|
Character tables |
| 111 |
|
|
---------------- |
| 112 |
|
|
|
| 113 |
nigel |
25 |
PCRE uses four tables for manipulating and identifying characters. The final |
| 114 |
|
|
argument of the pcre_compile() function is a pointer to a block of memory |
| 115 |
|
|
containing the concatenated tables. A call to pcre_maketables() is used to |
| 116 |
|
|
generate a set of tables in the current locale. However, if the final argument |
| 117 |
|
|
is passed as NULL, a set of default tables that is built into the binary is |
| 118 |
|
|
used. |
| 119 |
nigel |
3 |
|
| 120 |
nigel |
25 |
The source file called chartables.c contains the default set of tables. This is |
| 121 |
nigel |
27 |
not supplied in the distribution, but is built by the program dftables |
| 122 |
|
|
(compiled from dftables.c), which uses the ANSI C character handling functions |
| 123 |
nigel |
25 |
such as isalnum(), isalpha(), isupper(), islower(), etc. to build the table |
| 124 |
|
|
sources. This means that the default C locale set your system will control the |
| 125 |
|
|
contents of the tables. You can change the default tables by editing |
| 126 |
|
|
chartables.c and then re-building PCRE. If you do this, you should probably |
| 127 |
|
|
also edit Makefile to ensure that the file doesn't ever get re-generated. |
| 128 |
nigel |
3 |
|
| 129 |
nigel |
25 |
The first two 256-byte tables provide lower casing and case flipping functions, |
| 130 |
|
|
respectively. The next table consists of three 32-byte bit maps which identify |
| 131 |
|
|
digits, "word" characters, and white space, respectively. These are used when |
| 132 |
|
|
building 32-byte bit maps that represent character classes. |
| 133 |
|
|
|
| 134 |
|
|
The final 256-byte table has bits indicating various character types, as |
| 135 |
nigel |
3 |
follows: |
| 136 |
|
|
|
| 137 |
|
|
1 white space character |
| 138 |
|
|
2 letter |
| 139 |
|
|
4 decimal digit |
| 140 |
|
|
8 hexadecimal digit |
| 141 |
|
|
16 alphanumeric or '_' |
| 142 |
|
|
128 regular expression metacharacter or binary zero |
| 143 |
|
|
|
| 144 |
|
|
You should not alter the set of characters that contain the 128 bit, as that |
| 145 |
|
|
will cause PCRE to malfunction. |
| 146 |
|
|
|
| 147 |
|
|
|
| 148 |
|
|
The pcretest program |
| 149 |
|
|
-------------------- |
| 150 |
|
|
|
| 151 |
|
|
This program is intended for testing PCRE, but it can also be used for |
| 152 |
|
|
experimenting with regular expressions. |
| 153 |
|
|
|
| 154 |
|
|
If it is given two filename arguments, it reads from the first and writes to |
| 155 |
|
|
the second. If it is given only one filename argument, it reads from that file |
| 156 |
|
|
and writes to stdout. Otherwise, it reads from stdin and writes to stdout, and |
| 157 |
|
|
prompts for each line of input. |
| 158 |
|
|
|
| 159 |
|
|
The program handles any number of sets of input on a single input file. Each |
| 160 |
|
|
set starts with a regular expression, and continues with any number of data |
| 161 |
|
|
lines to be matched against the pattern. An empty line signals the end of the |
| 162 |
|
|
set. The regular expressions are given enclosed in any non-alphameric |
| 163 |
nigel |
29 |
delimiters other than backslash, for example |
| 164 |
nigel |
3 |
|
| 165 |
|
|
/(a|bc)x+yz/ |
| 166 |
|
|
|
| 167 |
nigel |
29 |
White space before the initial delimiter is ignored. A regular expression may |
| 168 |
|
|
be continued over several input lines, in which case the newline characters are |
| 169 |
|
|
included within it. See the testinput files for many examples. It is possible |
| 170 |
|
|
to include the delimiter within the pattern by escaping it, for example |
| 171 |
nigel |
3 |
|
| 172 |
nigel |
29 |
/abc\/def/ |
| 173 |
|
|
|
| 174 |
|
|
If you do so, the escape and the delimiter form part of the pattern, but since |
| 175 |
|
|
delimiters are always non-alphameric, this does not affect its interpretation. |
| 176 |
|
|
If the terminating delimiter is immediately followed by a backslash, for |
| 177 |
|
|
example, |
| 178 |
|
|
|
| 179 |
|
|
/abc/\ |
| 180 |
|
|
|
| 181 |
|
|
then a backslash is added to the end of the pattern. This provides a way of |
| 182 |
|
|
testing the error condition that arises if a pattern finishes with a backslash, |
| 183 |
|
|
because |
| 184 |
|
|
|
| 185 |
|
|
/abc\/ |
| 186 |
|
|
|
| 187 |
|
|
is interpreted as the first line of a pattern that starts with "abc/", causing |
| 188 |
|
|
pcretest to read the next line as a continuation of the regular expression. |
| 189 |
|
|
|
| 190 |
|
|
The pattern may be followed by i, m, s, or x to set the PCRE_CASELESS, |
| 191 |
|
|
PCRE_MULTILINE, PCRE_DOTALL, or PCRE_EXTENDED options, respectively. These |
| 192 |
|
|
options have the same effect as they do in Perl. |
| 193 |
|
|
|
| 194 |
nigel |
3 |
There are also some upper case options that do not match Perl options: /A, /E, |
| 195 |
|
|
and /X set PCRE_ANCHORED, PCRE_DOLLAR_ENDONLY, and PCRE_EXTRA respectively. |
| 196 |
|
|
|
| 197 |
nigel |
25 |
The /L option must be followed directly by the name of a locale, for example, |
| 198 |
|
|
|
| 199 |
|
|
/pattern/Lfr |
| 200 |
|
|
|
| 201 |
|
|
For this reason, it must be the last option letter. The given locale is set, |
| 202 |
|
|
pcre_maketables() is called to build a set of character tables for the locale, |
| 203 |
|
|
and this is then passed to pcre_compile() when compiling the regular |
| 204 |
|
|
expression. Without an /L option, NULL is passed as the tables pointer; that |
| 205 |
|
|
is, /L applies only to the expression on which it appears. |
| 206 |
|
|
|
| 207 |
|
|
The /I option requests that pcretest output information about the compiled |
| 208 |
|
|
expression (whether it is anchored, has a fixed first character, and so on). It |
| 209 |
|
|
does this by calling pcre_info() after compiling an expression, and outputting |
| 210 |
|
|
the information it gets back. If the pattern is studied, the results of that |
| 211 |
|
|
are also output. |
| 212 |
|
|
|
| 213 |
|
|
The /D option is a PCRE debugging feature, which also assumes /I. It causes the |
| 214 |
|
|
internal form of compiled regular expressions to be output after compilation. |
| 215 |
|
|
|
| 216 |
|
|
The /S option causes pcre_study() to be called after the expression has been |
| 217 |
|
|
compiled, and the results used when the expression is matched. |
| 218 |
|
|
|
| 219 |
nigel |
31 |
The /M option causes information about the size of memory block used to hold |
| 220 |
|
|
the compile pattern to be output. |
| 221 |
|
|
|
| 222 |
nigel |
3 |
Finally, the /P option causes pcretest to call PCRE via the POSIX wrapper API |
| 223 |
|
|
rather than its native API. When this is done, all other options except /i and |
| 224 |
|
|
/m are ignored. REG_ICASE is set if /i is present, and REG_NEWLINE is set if /m |
| 225 |
|
|
is present. The wrapper functions force PCRE_DOLLAR_ENDONLY always, and |
| 226 |
|
|
PCRE_DOTALL unless REG_NEWLINE is set. |
| 227 |
|
|
|
| 228 |
|
|
Before each data line is passed to pcre_exec(), leading and trailing whitespace |
| 229 |
|
|
is removed, and it is then scanned for \ escapes. The following are recognized: |
| 230 |
|
|
|
| 231 |
|
|
\a alarm (= BEL) |
| 232 |
|
|
\b backspace |
| 233 |
|
|
\e escape |
| 234 |
|
|
\f formfeed |
| 235 |
|
|
\n newline |
| 236 |
|
|
\r carriage return |
| 237 |
|
|
\t tab |
| 238 |
|
|
\v vertical tab |
| 239 |
|
|
\nnn octal character (up to 3 octal digits) |
| 240 |
|
|
\xhh hexadecimal character (up to 2 hex digits) |
| 241 |
|
|
|
| 242 |
|
|
\A pass the PCRE_ANCHORED option to pcre_exec() |
| 243 |
|
|
\B pass the PCRE_NOTBOL option to pcre_exec() |
| 244 |
nigel |
29 |
\Cdd call pcre_copy_substring() for substring dd after a successful match |
| 245 |
|
|
(any decimal number less than 32) |
| 246 |
|
|
\Gdd call pcre_get_substring() for substring dd after a successful match |
| 247 |
|
|
(any decimal number less than 32) |
| 248 |
|
|
\L call pcre_get_substringlist() after a successful match |
| 249 |
nigel |
3 |
\Odd set the size of the output vector passed to pcre_exec() to dd |
| 250 |
|
|
(any number of decimal digits) |
| 251 |
|
|
\Z pass the PCRE_NOTEOL option to pcre_exec() |
| 252 |
|
|
|
| 253 |
|
|
A backslash followed by anything else just escapes the anything else. If the |
| 254 |
|
|
very last character is a backslash, it is ignored. This gives a way of passing |
| 255 |
|
|
an empty line as data, since a real empty line terminates the data input. |
| 256 |
|
|
|
| 257 |
|
|
If /P was present on the regex, causing the POSIX wrapper API to be used, only |
| 258 |
|
|
\B, and \Z have any effect, causing REG_NOTBOL and REG_NOTEOL to be passed to |
| 259 |
|
|
regexec() respectively. |
| 260 |
|
|
|
| 261 |
nigel |
29 |
When a match succeeds, pcretest outputs the list of captured substrings that |
| 262 |
nigel |
3 |
pcre_exec() returns, starting with number 0 for the string that matched the |
| 263 |
|
|
whole pattern. Here is an example of an interactive pcretest run. |
| 264 |
|
|
|
| 265 |
|
|
$ pcretest |
| 266 |
|
|
Testing Perl-Compatible Regular Expressions |
| 267 |
|
|
PCRE version 0.90 08-Sep-1997 |
| 268 |
|
|
|
| 269 |
nigel |
23 |
re> /^abc(\d+)/ |
| 270 |
|
|
data> abc123 |
| 271 |
|
|
0: abc123 |
| 272 |
|
|
1: 123 |
| 273 |
|
|
data> xyz |
| 274 |
nigel |
3 |
No match |
| 275 |
|
|
|
| 276 |
nigel |
29 |
If any of \C, \G, or \L are present in a data line that is successfully |
| 277 |
|
|
matched, the substrings extracted by the convenience functions are output with |
| 278 |
|
|
C, G, or L after the string number instead of a colon. This is in addition to |
| 279 |
|
|
the normal full list. The string length (that is, the return from the |
| 280 |
|
|
extraction function) is given in parentheses after each string for \C and \G. |
| 281 |
|
|
|
| 282 |
nigel |
3 |
Note that while patterns can be continued over several lines (a plain ">" |
| 283 |
|
|
prompt is used for continuations), data lines may not. However newlines can be |
| 284 |
|
|
included in data by means of the \n escape. |
| 285 |
|
|
|
| 286 |
|
|
If the -p option is given to pcretest, it is equivalent to adding /P to each |
| 287 |
|
|
regular expression: the POSIX wrapper API is used to call PCRE. None of the |
| 288 |
|
|
following flags has any effect in this case. |
| 289 |
|
|
|
| 290 |
|
|
If the option -d is given to pcretest, it is equivalent to adding /D to each |
| 291 |
|
|
regular expression: the internal form is output after compilation. |
| 292 |
|
|
|
| 293 |
nigel |
25 |
If the option -i is given to pcretest, it is equivalent to adding /I to each |
| 294 |
|
|
regular expression: information about the compiled pattern is given after |
| 295 |
|
|
compilation. |
| 296 |
nigel |
3 |
|
| 297 |
nigel |
31 |
If the option -m is given to pcretest, it outputs the size of each compiled |
| 298 |
|
|
pattern after it has been compiled. It is equivalent to adding /M to each |
| 299 |
|
|
regular expression. For compatibility with earlier versions of pcretest, -s is |
| 300 |
|
|
a synonym for -m. |
| 301 |
nigel |
3 |
|
| 302 |
nigel |
29 |
If the -t option is given, each compile, study, and match is run 20000 times |
| 303 |
nigel |
3 |
while being timed, and the resulting time per compile or match is output in |
| 304 |
|
|
milliseconds. Do not set -t with -s, because you will then get the size output |
| 305 |
nigel |
29 |
20000 times and the timing will be distorted. If you want to change the number |
| 306 |
nigel |
23 |
of repetitions used for timing, edit the definition of LOOPREPEAT at the top of |
| 307 |
|
|
pcretest.c |
| 308 |
nigel |
3 |
|
| 309 |
|
|
|
| 310 |
|
|
|
| 311 |
|
|
The perltest program |
| 312 |
|
|
-------------------- |
| 313 |
|
|
|
| 314 |
|
|
The perltest program tests Perl's regular expressions; it has the same |
| 315 |
|
|
specification as pcretest, and so can be given identical input, except that |
| 316 |
nigel |
23 |
input patterns can be followed only by Perl's lower case options. The contents |
| 317 |
|
|
of testinput and testinput3 meet this condition. |
| 318 |
nigel |
3 |
|
| 319 |
|
|
The data lines are processed as Perl strings, so if they contain $ or @ |
| 320 |
|
|
characters, these have to be escaped. For this reason, all such characters in |
| 321 |
|
|
the testinput file are escaped so that it can be used for perltest as well as |
| 322 |
|
|
for pcretest, and the special upper case options such as /A that pcretest |
| 323 |
|
|
recognizes are not used in this file. The output should be identical, apart |
| 324 |
|
|
from the initial identifying banner. |
| 325 |
|
|
|
| 326 |
nigel |
25 |
The testinput2 and testinput4 files are not suitable for feeding to Perltest, |
| 327 |
|
|
since they do make use of the special upper case options and escapes that |
| 328 |
|
|
pcretest uses to test some features of PCRE. The first of these files also |
| 329 |
|
|
contains malformed regular expressions, in order to check that PCRE diagnoses |
| 330 |
|
|
them correctly. |
| 331 |
nigel |
3 |
|
| 332 |
|
|
Philip Hazel <ph10@cam.ac.uk> |
| 333 |
nigel |
29 |
February 1999 |