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