| 1 |
README file for PCRE (Perl-compatible regular expressions)
|
| 2 |
----------------------------------------------------------
|
| 3 |
|
| 4 |
*******************************************************************************
|
| 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 |
*******************************************************************************
|
| 12 |
|
| 13 |
The distribution should contain the following files:
|
| 14 |
|
| 15 |
ChangeLog log of changes to the code
|
| 16 |
Makefile for building PCRE
|
| 17 |
README this file
|
| 18 |
RunTest a shell script for running tests
|
| 19 |
Tech.Notes notes on the encoding
|
| 20 |
pcre.3 man page for the functions
|
| 21 |
pcreposix.3 man page for the POSIX wrapper API
|
| 22 |
maketables.c auxiliary program for building chartables.c
|
| 23 |
study.c ) source of
|
| 24 |
pcre.c ) the functions
|
| 25 |
pcreposix.c )
|
| 26 |
pcre.h header for the external API
|
| 27 |
pcreposix.h header for the external POSIX wrapper API
|
| 28 |
internal.h header for internal use
|
| 29 |
pcretest.c test program
|
| 30 |
pgrep.1 man page for pgrep
|
| 31 |
pgrep.c source of a grep utility that uses PCRE
|
| 32 |
perltest Perl test program
|
| 33 |
testinput test data, compatible with Perl 5.004 and 5.005
|
| 34 |
testinput2 test data for error messages and non-Perl things
|
| 35 |
testinput3 test data, compatible with Perl 5.005
|
| 36 |
testoutput test results corresponding to testinput
|
| 37 |
testoutput2 test results corresponding to testinput2
|
| 38 |
testoutput3 test results corresponding to testinpug3
|
| 39 |
|
| 40 |
To build PCRE, edit Makefile for your system (it is a fairly simple make file,
|
| 41 |
and there are some comments at the top) and then run it. It builds two
|
| 42 |
libraries called libpcre.a and libpcreposix.a, a test program called pcretest,
|
| 43 |
and the pgrep command.
|
| 44 |
|
| 45 |
To test PCRE, run the RunTest script in the pcre directory. This runs pcretest
|
| 46 |
on each of the testinput files in turn, and compares the output with the
|
| 47 |
contents of the corresponding testoutput file. A file called testtry is used to
|
| 48 |
hold the output from pcretest (which is documented below).
|
| 49 |
|
| 50 |
To run pcretest on just one of the test files, give its number as an argument
|
| 51 |
to RunTest, for example:
|
| 52 |
|
| 53 |
RunTest 3
|
| 54 |
|
| 55 |
The first and third test files can also be fed directly into the perltest
|
| 56 |
program to check that Perl gives the same results. The third file requires the
|
| 57 |
additional features of release 5.005, which is why it is kept separate from the
|
| 58 |
main test input, which needs only Perl 5.004. In the long run, when 5.005 is
|
| 59 |
widespread, these two test files may get amalgamated.
|
| 60 |
|
| 61 |
The second set of tests check pcre_info(), pcre_study(), error detection and
|
| 62 |
run-time flags that are specific to PCRE, as well as the POSIX wrapper API.
|
| 63 |
|
| 64 |
To install PCRE, copy libpcre.a to any suitable library directory (e.g.
|
| 65 |
/usr/local/lib), pcre.h to any suitable include directory (e.g.
|
| 66 |
/usr/local/include), and pcre.3 to any suitable man directory (e.g.
|
| 67 |
/usr/local/man/man3).
|
| 68 |
|
| 69 |
To install the pgrep command, copy it to any suitable binary directory, (e.g.
|
| 70 |
/usr/local/bin) and pgrep.1 to any suitable man directory (e.g.
|
| 71 |
/usr/local/man/man1).
|
| 72 |
|
| 73 |
PCRE has its own native API, but a set of "wrapper" functions that are based on
|
| 74 |
the POSIX API are also supplied in the library libpcreposix.a. Note that this
|
| 75 |
just provides a POSIX calling interface to PCRE: the regular expressions
|
| 76 |
themselves still follow Perl syntax and semantics. The header file
|
| 77 |
for the POSIX-style functions is called pcreposix.h. The official POSIX name is
|
| 78 |
regex.h, but I didn't want to risk possible problems with existing files of
|
| 79 |
that name by distributing it that way. To use it with an existing program that
|
| 80 |
uses the POSIX API, it will have to be renamed or pointed at by a link.
|
| 81 |
|
| 82 |
|
| 83 |
Character tables
|
| 84 |
----------------
|
| 85 |
|
| 86 |
PCRE uses four tables for manipulating and identifying characters. These are
|
| 87 |
compiled from a source file called chartables.c. This is not supplied in
|
| 88 |
the distribution, but is built by the program maketables (compiled from
|
| 89 |
maketables.c), which uses the ANSI C character handling functions such as
|
| 90 |
isalnum(), isalpha(), isupper(), islower(), etc. to build the table sources.
|
| 91 |
This means that the default C locale set in your system may affect the contents
|
| 92 |
of the tables. You can change the tables by editing chartables.c and then
|
| 93 |
re-building PCRE. If you do this, you should probably also edit Makefile to
|
| 94 |
ensure that the file doesn't ever get re-generated.
|
| 95 |
|
| 96 |
The first two tables pcre_lcc[] and pcre_fcc[] provide lower casing and a
|
| 97 |
case flipping functions, respectively. The pcre_cbits[] table consists of four
|
| 98 |
32-byte bit maps which identify digits, letters, "word" characters, and white
|
| 99 |
space, respectively. These are used when building 32-byte bit maps that
|
| 100 |
represent character classes.
|
| 101 |
|
| 102 |
The pcre_ctypes[] table has bits indicating various character types, as
|
| 103 |
follows:
|
| 104 |
|
| 105 |
1 white space character
|
| 106 |
2 letter
|
| 107 |
4 decimal digit
|
| 108 |
8 hexadecimal digit
|
| 109 |
16 alphanumeric or '_'
|
| 110 |
128 regular expression metacharacter or binary zero
|
| 111 |
|
| 112 |
You should not alter the set of characters that contain the 128 bit, as that
|
| 113 |
will cause PCRE to malfunction.
|
| 114 |
|
| 115 |
|
| 116 |
The pcretest program
|
| 117 |
--------------------
|
| 118 |
|
| 119 |
This program is intended for testing PCRE, but it can also be used for
|
| 120 |
experimenting with regular expressions.
|
| 121 |
|
| 122 |
If it is given two filename arguments, it reads from the first and writes to
|
| 123 |
the second. If it is given only one filename argument, it reads from that file
|
| 124 |
and writes to stdout. Otherwise, it reads from stdin and writes to stdout, and
|
| 125 |
prompts for each line of input.
|
| 126 |
|
| 127 |
The program handles any number of sets of input on a single input file. Each
|
| 128 |
set starts with a regular expression, and continues with any number of data
|
| 129 |
lines to be matched against the pattern. An empty line signals the end of the
|
| 130 |
set. The regular expressions are given enclosed in any non-alphameric
|
| 131 |
delimiters, for example
|
| 132 |
|
| 133 |
/(a|bc)x+yz/
|
| 134 |
|
| 135 |
and may be followed by i, m, s, or x to set the PCRE_CASELESS, PCRE_MULTILINE,
|
| 136 |
PCRE_DOTALL, or PCRE_EXTENDED options, respectively. These options have the
|
| 137 |
same effect as they do in Perl.
|
| 138 |
|
| 139 |
There are also some upper case options that do not match Perl options: /A, /E,
|
| 140 |
and /X set PCRE_ANCHORED, PCRE_DOLLAR_ENDONLY, and PCRE_EXTRA respectively.
|
| 141 |
The /D option is a PCRE debugging feature. It causes the internal form of
|
| 142 |
compiled regular expressions to be output after compilation. The /S option
|
| 143 |
causes pcre_study() to be called after the expression has been compiled, and
|
| 144 |
the results used when the expression is matched.
|
| 145 |
|
| 146 |
Finally, the /P option causes pcretest to call PCRE via the POSIX wrapper API
|
| 147 |
rather than its native API. When this is done, all other options except /i and
|
| 148 |
/m are ignored. REG_ICASE is set if /i is present, and REG_NEWLINE is set if /m
|
| 149 |
is present. The wrapper functions force PCRE_DOLLAR_ENDONLY always, and
|
| 150 |
PCRE_DOTALL unless REG_NEWLINE is set.
|
| 151 |
|
| 152 |
A regular expression can extend over several lines of input; the newlines are
|
| 153 |
included in it. See the testinput files for many examples.
|
| 154 |
|
| 155 |
Before each data line is passed to pcre_exec(), leading and trailing whitespace
|
| 156 |
is removed, and it is then scanned for \ escapes. The following are recognized:
|
| 157 |
|
| 158 |
\a alarm (= BEL)
|
| 159 |
\b backspace
|
| 160 |
\e escape
|
| 161 |
\f formfeed
|
| 162 |
\n newline
|
| 163 |
\r carriage return
|
| 164 |
\t tab
|
| 165 |
\v vertical tab
|
| 166 |
\nnn octal character (up to 3 octal digits)
|
| 167 |
\xhh hexadecimal character (up to 2 hex digits)
|
| 168 |
|
| 169 |
\A pass the PCRE_ANCHORED option to pcre_exec()
|
| 170 |
\B pass the PCRE_NOTBOL option to pcre_exec()
|
| 171 |
\Odd set the size of the output vector passed to pcre_exec() to dd
|
| 172 |
(any number of decimal digits)
|
| 173 |
\Z pass the PCRE_NOTEOL option to pcre_exec()
|
| 174 |
|
| 175 |
A backslash followed by anything else just escapes the anything else. If the
|
| 176 |
very last character is a backslash, it is ignored. This gives a way of passing
|
| 177 |
an empty line as data, since a real empty line terminates the data input.
|
| 178 |
|
| 179 |
If /P was present on the regex, causing the POSIX wrapper API to be used, only
|
| 180 |
\B, and \Z have any effect, causing REG_NOTBOL and REG_NOTEOL to be passed to
|
| 181 |
regexec() respectively.
|
| 182 |
|
| 183 |
When a match succeeds, pcretest outputs the list of identified substrings that
|
| 184 |
pcre_exec() returns, starting with number 0 for the string that matched the
|
| 185 |
whole pattern. Here is an example of an interactive pcretest run.
|
| 186 |
|
| 187 |
$ pcretest
|
| 188 |
Testing Perl-Compatible Regular Expressions
|
| 189 |
PCRE version 0.90 08-Sep-1997
|
| 190 |
|
| 191 |
re> /^abc(\d+)/
|
| 192 |
data> abc123
|
| 193 |
0: abc123
|
| 194 |
1: 123
|
| 195 |
data> xyz
|
| 196 |
No match
|
| 197 |
|
| 198 |
Note that while patterns can be continued over several lines (a plain ">"
|
| 199 |
prompt is used for continuations), data lines may not. However newlines can be
|
| 200 |
included in data by means of the \n escape.
|
| 201 |
|
| 202 |
If the -p option is given to pcretest, it is equivalent to adding /P to each
|
| 203 |
regular expression: the POSIX wrapper API is used to call PCRE. None of the
|
| 204 |
following flags has any effect in this case.
|
| 205 |
|
| 206 |
If the option -d is given to pcretest, it is equivalent to adding /D to each
|
| 207 |
regular expression: the internal form is output after compilation.
|
| 208 |
|
| 209 |
If the option -i (for "information") is given to pcretest, it calls pcre_info()
|
| 210 |
after compiling an expression, and outputs the information it gets back. If the
|
| 211 |
pattern is studied, the results of that are also output.
|
| 212 |
|
| 213 |
If the option -s is given to pcretest, it outputs the size of each compiled
|
| 214 |
pattern after it has been compiled.
|
| 215 |
|
| 216 |
If the -t option is given, each compile, study, and match is run 10000 times
|
| 217 |
while being timed, and the resulting time per compile or match is output in
|
| 218 |
milliseconds. Do not set -t with -s, because you will then get the size output
|
| 219 |
10000 times and the timing will be distorted. If you want to change the number
|
| 220 |
of repetitions used for timing, edit the definition of LOOPREPEAT at the top of
|
| 221 |
pcretest.c
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
The perltest program
|
| 226 |
--------------------
|
| 227 |
|
| 228 |
The perltest program tests Perl's regular expressions; it has the same
|
| 229 |
specification as pcretest, and so can be given identical input, except that
|
| 230 |
input patterns can be followed only by Perl's lower case options. The contents
|
| 231 |
of testinput and testinput3 meet this condition.
|
| 232 |
|
| 233 |
The data lines are processed as Perl strings, so if they contain $ or @
|
| 234 |
characters, these have to be escaped. For this reason, all such characters in
|
| 235 |
the testinput file are escaped so that it can be used for perltest as well as
|
| 236 |
for pcretest, and the special upper case options such as /A that pcretest
|
| 237 |
recognizes are not used in this file. The output should be identical, apart
|
| 238 |
from the initial identifying banner.
|
| 239 |
|
| 240 |
The testinput2 file is not suitable for feeding to Perltest, since it does
|
| 241 |
make use of the special upper case options and escapes that pcretest uses to
|
| 242 |
test some features of PCRE. It also contains malformed regular expressions, in
|
| 243 |
order to check that PCRE diagnoses them correctly.
|
| 244 |
|
| 245 |
Philip Hazel <ph10@cam.ac.uk>
|
| 246 |
September 1998
|