1 |
.TH PCREPRECOMPILE 3
|
2 |
.SH NAME
|
3 |
PCRE - Perl-compatible regular expressions
|
4 |
.SH "SAVING AND RE-USING PRECOMPILED PCRE PATTERNS"
|
5 |
.rs
|
6 |
.sp
|
7 |
If you are running an application that uses a large number of regular
|
8 |
expression patterns, it may be useful to store them in a precompiled form
|
9 |
instead of having to compile them every time the application is run.
|
10 |
If you are not using any private character tables (see the
|
11 |
.\" HREF
|
12 |
\fBpcre_maketables()\fP
|
13 |
.\"
|
14 |
documentation), this is relatively straightforward. If you are using private
|
15 |
tables, it is a little bit more complicated.
|
16 |
.P
|
17 |
If you save compiled patterns to a file, you can copy them to a different host
|
18 |
and run them there. This works even if the new host has the opposite endianness
|
19 |
to the one on which the patterns were compiled. There may be a small
|
20 |
performance penalty, but it should be insignificant. However, compiling regular
|
21 |
expressions with one version of PCRE for use with a different version is not
|
22 |
guaranteed to work and may cause crashes.
|
23 |
.
|
24 |
.
|
25 |
.SH "SAVING A COMPILED PATTERN"
|
26 |
.rs
|
27 |
.sh
|
28 |
The value returned by \fBpcre_compile()\fP points to a single block of memory
|
29 |
that holds the compiled pattern and associated data. You can find the length of
|
30 |
this block in bytes by calling \fBpcre_fullinfo()\fP with an argument of
|
31 |
PCRE_INFO_SIZE. You can then save the data in any appropriate manner. Here is
|
32 |
sample code that compiles a pattern and writes it to a file. It assumes that
|
33 |
the variable \fIfd\fP refers to a file that is open for output:
|
34 |
.sp
|
35 |
int erroroffset, rc, size;
|
36 |
char *error;
|
37 |
pcre *re;
|
38 |
.sp
|
39 |
re = pcre_compile("my pattern", 0, &error, &erroroffset, NULL);
|
40 |
if (re == NULL) { ... handle errors ... }
|
41 |
rc = pcre_fullinfo(re, NULL, PCRE_INFO_SIZE, &size);
|
42 |
if (rc < 0) { ... handle errors ... }
|
43 |
rc = fwrite(re, 1, size, fd);
|
44 |
if (rc != size) { ... handle errors ... }
|
45 |
.sp
|
46 |
In this example, the bytes that comprise the compiled pattern are copied
|
47 |
exactly. Note that this is binary data that may contain any of the 256 possible
|
48 |
byte values. On systems that make a distinction between binary and non-binary
|
49 |
data, be sure that the file is opened for binary output.
|
50 |
.P
|
51 |
If you want to write more than one pattern to a file, you will have to devise a
|
52 |
way of separating them. For binary data, preceding each pattern with its length
|
53 |
is probably the most straightforward approach. Another possibility is to write
|
54 |
out the data in hexadecimal instead of binary, one pattern to a line.
|
55 |
.P
|
56 |
Saving compiled patterns in a file is only one possible way of storing them for
|
57 |
later use. They could equally well be saved in a database, or in the memory of
|
58 |
some daemon process that passes them via sockets to the processes that want
|
59 |
them.
|
60 |
.P
|
61 |
If the pattern has been studied, it is also possible to save the study data in
|
62 |
a similar way to the compiled pattern itself. When studying generates
|
63 |
additional information, \fBpcre_study()\fP returns a pointer to a
|
64 |
\fBpcre_extra\fP data block. Its format is defined in the
|
65 |
.\" HTML <a href="pcreapi.html#extradata">
|
66 |
.\" </a>
|
67 |
section on matching a pattern
|
68 |
.\"
|
69 |
in the
|
70 |
.\" HREF
|
71 |
\fBpcreapi\fP
|
72 |
.\"
|
73 |
documentation. The \fIstudy_data\fP field points to the binary study data, and
|
74 |
this is what you must save (not the \fBpcre_extra\fP block itself). The length
|
75 |
of the study data can be obtained by calling \fBpcre_fullinfo()\fP with an
|
76 |
argument of PCRE_INFO_STUDYSIZE. Remember to check that \fBpcre_study()\fP did
|
77 |
return a non-NULL value before trying to save the study data.
|
78 |
.
|
79 |
.
|
80 |
.SH "RE-USING A PRECOMPILED PATTERN"
|
81 |
.rs
|
82 |
.sp
|
83 |
Re-using a precompiled pattern is straightforward. Having reloaded it into main
|
84 |
memory, you pass its pointer to \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP in
|
85 |
the usual way. This should work even on another host, and even if that host has
|
86 |
the opposite endianness to the one where the pattern was compiled.
|
87 |
.P
|
88 |
However, if you passed a pointer to custom character tables when the pattern
|
89 |
was compiled (the \fItableptr\fP argument of \fBpcre_compile()\fP), you must
|
90 |
now pass a similar pointer to \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP,
|
91 |
because the value saved with the compiled pattern will obviously be nonsense. A
|
92 |
field in a \fBpcre_extra()\fP block is used to pass this data, as described in
|
93 |
the
|
94 |
.\" HTML <a href="pcreapi.html#extradata">
|
95 |
.\" </a>
|
96 |
section on matching a pattern
|
97 |
.\"
|
98 |
in the
|
99 |
.\" HREF
|
100 |
\fBpcreapi\fP
|
101 |
.\"
|
102 |
documentation.
|
103 |
.P
|
104 |
If you did not provide custom character tables when the pattern was compiled,
|
105 |
the pointer in the compiled pattern is NULL, which causes \fBpcre_exec()\fP to
|
106 |
use PCRE's internal tables. Thus, you do not need to take any special action at
|
107 |
run time in this case.
|
108 |
.P
|
109 |
If you saved study data with the compiled pattern, you need to create your own
|
110 |
\fBpcre_extra\fP data block and set the \fIstudy_data\fP field to point to the
|
111 |
reloaded study data. You must also set the PCRE_EXTRA_STUDY_DATA bit in the
|
112 |
\fIflags\fP field to indicate that study data is present. Then pass the
|
113 |
\fBpcre_extra\fP block to \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP in the
|
114 |
usual way.
|
115 |
.
|
116 |
.
|
117 |
.SH "COMPATIBILITY WITH DIFFERENT PCRE RELEASES"
|
118 |
.rs
|
119 |
.sp
|
120 |
In general, it is safest to recompile all saved patterns when you update to a
|
121 |
new PCRE release, though not all updates actually require this. Recompiling is
|
122 |
definitely needed for release 7.2.
|
123 |
.
|
124 |
.
|
125 |
.
|
126 |
.SH AUTHOR
|
127 |
.rs
|
128 |
.sp
|
129 |
.nf
|
130 |
Philip Hazel
|
131 |
University Computing Service
|
132 |
Cambridge CB2 3QH, England.
|
133 |
.fi
|
134 |
.
|
135 |
.
|
136 |
.SH REVISION
|
137 |
.rs
|
138 |
.sp
|
139 |
.nf
|
140 |
Last updated: 13 June 2007
|
141 |
Copyright (c) 1997-2007 University of Cambridge.
|
142 |
.fi
|