1PCREPRECOMPILE(3) Library Functions Manual PCREPRECOMPILE(3)
2
3
4
6 PCRE - Perl-compatible regular expressions
7
9
10 If you are running an application that uses a large number of regular
11 expression patterns, it may be useful to store them in a precompiled
12 form instead of having to compile them every time the application is
13 run. If you are not using any private character tables (see the
14 pcre_maketables() documentation), this is relatively straightforward.
15 If you are using private tables, it is a little bit more complicated.
16 However, if you are using the just-in-time optimization feature, it is
17 not possible to save and reload the JIT data.
18
19 If you save compiled patterns to a file, you can copy them to a differ‐
20 ent host and run them there. If the two hosts have different endianness
21 (byte order), you should run the pcre[16|32]_pat‐
22 tern_to_host_byte_order() function on the new host before trying to
23 match the pattern. The matching functions return PCRE_ERROR_BADENDIAN‐
24 NESS if they detect a pattern with the wrong endianness.
25
26 Compiling regular expressions with one version of PCRE for use with a
27 different version is not guaranteed to work and may cause crashes, and
28 saving and restoring a compiled pattern loses any JIT optimization
29 data.
30
32
33 The value returned by pcre[16|32]_compile() points to a single block of
34 memory that holds the compiled pattern and associated data. You can
35 find the length of this block in bytes by calling
36 pcre[16|32]_fullinfo() with an argument of PCRE_INFO_SIZE. You can then
37 save the data in any appropriate manner. Here is sample code for the
38 8-bit library that compiles a pattern and writes it to a file. It
39 assumes that the variable fd refers to a file that is open for output:
40
41 int erroroffset, rc, size;
42 char *error;
43 pcre *re;
44
45 re = pcre_compile("my pattern", 0, &error, &erroroffset, NULL);
46 if (re == NULL) { ... handle errors ... }
47 rc = pcre_fullinfo(re, NULL, PCRE_INFO_SIZE, &size);
48 if (rc < 0) { ... handle errors ... }
49 rc = fwrite(re, 1, size, fd);
50 if (rc != size) { ... handle errors ... }
51
52 In this example, the bytes that comprise the compiled pattern are
53 copied exactly. Note that this is binary data that may contain any of
54 the 256 possible byte values. On systems that make a distinction
55 between binary and non-binary data, be sure that the file is opened for
56 binary output.
57
58 If you want to write more than one pattern to a file, you will have to
59 devise a way of separating them. For binary data, preceding each pat‐
60 tern with its length is probably the most straightforward approach.
61 Another possibility is to write out the data in hexadecimal instead of
62 binary, one pattern to a line.
63
64 Saving compiled patterns in a file is only one possible way of storing
65 them for later use. They could equally well be saved in a database, or
66 in the memory of some daemon process that passes them via sockets to
67 the processes that want them.
68
69 If the pattern has been studied, it is also possible to save the normal
70 study data in a similar way to the compiled pattern itself. However, if
71 the PCRE_STUDY_JIT_COMPILE was used, the just-in-time data that is cre‐
72 ated cannot be saved because it is too dependent on the current envi‐
73 ronment. When studying generates additional information,
74 pcre[16|32]_study() returns a pointer to a pcre[16|32]_extra data
75 block. Its format is defined in the section on matching a pattern in
76 the pcreapi documentation. The study_data field points to the binary
77 study data, and this is what you must save (not the pcre[16|32]_extra
78 block itself). The length of the study data can be obtained by calling
79 pcre[16|32]_fullinfo() with an argument of PCRE_INFO_STUDYSIZE. Remem‐
80 ber to check that pcre[16|32]_study() did return a non-NULL value
81 before trying to save the study data.
82
84
85 Re-using a precompiled pattern is straightforward. Having reloaded it
86 into main memory, called pcre[16|32]_pattern_to_host_byte_order() if
87 necessary, you pass its pointer to pcre[16|32]_exec() or
88 pcre[16|32]_dfa_exec() in the usual way.
89
90 However, if you passed a pointer to custom character tables when the
91 pattern was compiled (the tableptr argument of pcre[16|32]_compile()),
92 you must now pass a similar pointer to pcre[16|32]_exec() or
93 pcre[16|32]_dfa_exec(), because the value saved with the compiled pat‐
94 tern will obviously be nonsense. A field in a pcre[16|32]_extra() block
95 is used to pass this data, as described in the section on matching a
96 pattern in the pcreapi documentation.
97
98 Warning: The tables that pcre_exec() and pcre_dfa_exec() use must be
99 the same as those that were used when the pattern was compiled. If this
100 is not the case, the behaviour is undefined.
101
102 If you did not provide custom character tables when the pattern was
103 compiled, the pointer in the compiled pattern is NULL, which causes the
104 matching functions to use PCRE's internal tables. Thus, you do not need
105 to take any special action at run time in this case.
106
107 If you saved study data with the compiled pattern, you need to create
108 your own pcre[16|32]_extra data block and set the study_data field to
109 point to the reloaded study data. You must also set the
110 PCRE_EXTRA_STUDY_DATA bit in the flags field to indicate that study
111 data is present. Then pass the pcre[16|32]_extra block to the matching
112 function in the usual way. If the pattern was studied for just-in-time
113 optimization, that data cannot be saved, and so is lost by a
114 save/restore cycle.
115
117
118 In general, it is safest to recompile all saved patterns when you
119 update to a new PCRE release, though not all updates actually require
120 this.
121
123
124 Philip Hazel
125 University Computing Service
126 Cambridge CB2 3QH, England.
127
129
130 Last updated: 12 November 2013
131 Copyright (c) 1997-2013 University of Cambridge.
132
133
134
135PCRE 8.34 12 November 2013 PCREPRECOMPILE(3)