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
17 If you save compiled patterns to a file, you can copy them to a differ‐
18 ent host and run them there. This works even if the new host has the
19 opposite endianness to the one on which the patterns were compiled.
20 There may be a small performance penalty, but it should be insignifi‐
21 cant. However, compiling regular expressions with one version of PCRE
22 for use with a different version is not guaranteed to work and may
23 cause crashes.
24
26 The value returned by pcre_compile() points to a single block of memory
27 that holds the compiled pattern and associated data. You can find the
28 length of this block in bytes by calling pcre_fullinfo() with an argu‐
29 ment of PCRE_INFO_SIZE. You can then save the data in any appropriate
30 manner. Here is sample code that compiles a pattern and writes it to a
31 file. It assumes that the variable fd refers to a file that is open for
32 output:
33
34 int erroroffset, rc, size;
35 char *error;
36 pcre *re;
37
38 re = pcre_compile("my pattern", 0, &error, &erroroffset, NULL);
39 if (re == NULL) { ... handle errors ... }
40 rc = pcre_fullinfo(re, NULL, PCRE_INFO_SIZE, &size);
41 if (rc < 0) { ... handle errors ... }
42 rc = fwrite(re, 1, size, fd);
43 if (rc != size) { ... handle errors ... }
44
45 In this example, the bytes that comprise the compiled pattern are
46 copied exactly. Note that this is binary data that may contain any of
47 the 256 possible byte values. On systems that make a distinction
48 between binary and non-binary data, be sure that the file is opened for
49 binary output.
50
51 If you want to write more than one pattern to a file, you will have to
52 devise a way of separating them. For binary data, preceding each pat‐
53 tern with its length is probably the most straightforward approach.
54 Another possibility is to write out the data in hexadecimal instead of
55 binary, one pattern to a line.
56
57 Saving compiled patterns in a file is only one possible way of storing
58 them for later use. They could equally well be saved in a database, or
59 in the memory of some daemon process that passes them via sockets to
60 the processes that want them.
61
62 If the pattern has been studied, it is also possible to save the study
63 data in a similar way to the compiled pattern itself. When studying
64 generates additional information, pcre_study() returns a pointer to a
65 pcre_extra data block. Its format is defined in the section on matching
66 a pattern in the pcreapi documentation. The study_data field points to
67 the binary study data, and this is what you must save (not the
68 pcre_extra block itself). The length of the study data can be obtained
69 by calling pcre_fullinfo() with an argument of PCRE_INFO_STUDYSIZE.
70 Remember to check that pcre_study() did return a non-NULL value before
71 trying to save the study data.
72
74
75 Re-using a precompiled pattern is straightforward. Having reloaded it
76 into main memory, you pass its pointer to pcre_exec() or
77 pcre_dfa_exec() in the usual way. This should work even on another
78 host, and even if that host has the opposite endianness to the one
79 where the pattern was compiled.
80
81 However, if you passed a pointer to custom character tables when the
82 pattern was compiled (the tableptr argument of pcre_compile()), you
83 must now pass a similar pointer to pcre_exec() or pcre_dfa_exec(),
84 because the value saved with the compiled pattern will obviously be
85 nonsense. A field in a pcre_extra() block is used to pass this data, as
86 described in the section on matching a pattern in the pcreapi documen‐
87 tation.
88
89 If you did not provide custom character tables when the pattern was
90 compiled, the pointer in the compiled pattern is NULL, which causes
91 pcre_exec() to use PCRE's internal tables. Thus, you do not need to
92 take any special action at run time in this case.
93
94 If you saved study data with the compiled pattern, you need to create
95 your own pcre_extra data block and set the study_data field to point to
96 the reloaded study data. You must also set the PCRE_EXTRA_STUDY_DATA
97 bit in the flags field to indicate that study data is present. Then
98 pass the pcre_extra block to pcre_exec() or pcre_dfa_exec() in the
99 usual way.
100
102
103 In general, it is safest to recompile all saved patterns when you
104 update to a new PCRE release, though not all updates actually require
105 this. Recompiling is definitely needed for release 7.2.
106
108
109 Philip Hazel
110 University Computing Service
111 Cambridge CB2 3QH, England.
112
114
115 Last updated: 13 June 2007
116 Copyright (c) 1997-2007 University of Cambridge.
117
118
119
120 PCREPRECOMPILE(3)