1PCRE2UNICODE(3) Library Functions Manual PCRE2UNICODE(3)
2
3
4
6 PCRE - Perl-compatible regular expressions (revised API)
7
9
10 When PCRE2 is built with Unicode support (which is the default), it has
11 knowledge of Unicode character properties and can process text strings
12 in UTF-8, UTF-16, or UTF-32 format (depending on the code unit width).
13 However, by default, PCRE2 assumes that one code unit is one character.
14 To process a pattern as a UTF string, where a character may require
15 more than one code unit, you must call pcre2_compile() with the
16 PCRE2_UTF option flag, or the pattern must start with the sequence
17 (*UTF). When either of these is the case, both the pattern and any sub‐
18 ject strings that are matched against it are treated as UTF strings
19 instead of strings of individual one-code-unit characters. There are
20 also some other changes to the way characters are handled, as docu‐
21 mented below.
22
23 If you do not need Unicode support you can build PCRE2 without it, in
24 which case the library will be smaller.
25
27
28 When PCRE2 is built with Unicode support, the escape sequences \p{..},
29 \P{..}, and \X can be used. The Unicode properties that can be tested
30 are limited to the general category properties such as Lu for an upper
31 case letter or Nd for a decimal number, the Unicode script names such
32 as Arabic or Han, and the derived properties Any and L&. Full lists are
33 given in the pcre2pattern and pcre2syntax documentation. Only the short
34 names for properties are supported. For example, \p{L} matches a let‐
35 ter. Its Perl synonym, \p{Letter}, is not supported. Furthermore, in
36 Perl, many properties may optionally be prefixed by "Is", for compati‐
37 bility with Perl 5.6. PCRE2 does not support this.
38
40
41 Code points less than 256 can be specified in patterns by either braced
42 or unbraced hexadecimal escape sequences (for example, \x{b3} or \xb3).
43 Larger values have to use braced sequences. Unbraced octal code points
44 up to \777 are also recognized; larger ones can be coded using \o{...}.
45
46 The escape sequence \N{U+<hex digits>} is recognized as another way of
47 specifying a Unicode character by code point in a UTF mode. It is not
48 allowed in non-UTF modes.
49
50 In UTF modes, repeat quantifiers apply to complete UTF characters, not
51 to individual code units.
52
53 In UTF modes, the dot metacharacter matches one UTF character instead
54 of a single code unit.
55
56 The escape sequence \C can be used to match a single code unit in a UTF
57 mode, but its use can lead to some strange effects because it breaks up
58 multi-unit characters (see the description of \C in the pcre2pattern
59 documentation).
60
61 The use of \C is not supported by the alternative matching function
62 pcre2_dfa_match() when in UTF-8 or UTF-16 mode, that is, when a charac‐
63 ter may consist of more than one code unit. The use of \C in these
64 modes provokes a match-time error. Also, the JIT optimization does not
65 support \C in these modes. If JIT optimization is requested for a UTF-8
66 or UTF-16 pattern that contains \C, it will not succeed, and so when
67 pcre2_match() is called, the matching will be carried out by the normal
68 interpretive function.
69
70 The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly test
71 characters of any code value, but, by default, the characters that
72 PCRE2 recognizes as digits, spaces, or word characters remain the same
73 set as in non-UTF mode, all with code points less than 256. This
74 remains true even when PCRE2 is built to include Unicode support,
75 because to do otherwise would slow down matching in many common cases.
76 Note that this also applies to \b and \B, because they are defined in
77 terms of \w and \W. If you want to test for a wider sense of, say,
78 "digit", you can use explicit Unicode property tests such as \p{Nd}.
79 Alternatively, if you set the PCRE2_UCP option, the way that the char‐
80 acter escapes work is changed so that Unicode properties are used to
81 determine which characters match. There are more details in the section
82 on generic character types in the pcre2pattern documentation.
83
84 Similarly, characters that match the POSIX named character classes are
85 all low-valued characters, unless the PCRE2_UCP option is set.
86
87 However, the special horizontal and vertical white space matching
88 escapes (\h, \H, \v, and \V) do match all the appropriate Unicode char‐
89 acters, whether or not PCRE2_UCP is set.
90
92
93 Case-insensitive matching in a UTF mode makes use of Unicode properties
94 except for characters whose code points are less than 128 and that have
95 at most two case-equivalent values. For these, a direct table lookup is
96 used for speed. A few Unicode characters such as Greek sigma have more
97 than two code points that are case-equivalent, and these are treated as
98 such.
99
101
102 When the PCRE2_UTF option is set, the strings passed as patterns and
103 subjects are (by default) checked for validity on entry to the relevant
104 functions. If an invalid UTF string is passed, an negative error code
105 is returned. The code unit offset to the offending character can be
106 extracted from the match data block by calling pcre2_get_startchar(),
107 which is used for this purpose after a UTF error.
108
109 UTF-16 and UTF-32 strings can indicate their endianness by special code
110 knows as a byte-order mark (BOM). The PCRE2 functions do not handle
111 this, expecting strings to be in host byte order.
112
113 A UTF string is checked before any other processing takes place. In the
114 case of pcre2_match() and pcre2_dfa_match() calls with a non-zero
115 starting offset, the check is applied only to that part of the subject
116 that could be inspected during matching, and there is a check that the
117 starting offset points to the first code unit of a character or to the
118 end of the subject. If there are no lookbehind assertions in the pat‐
119 tern, the check starts at the starting offset. Otherwise, it starts at
120 the length of the longest lookbehind before the starting offset, or at
121 the start of the subject if there are not that many characters before
122 the starting offset. Note that the sequences \b and \B are one-charac‐
123 ter lookbehinds.
124
125 In addition to checking the format of the string, there is a check to
126 ensure that all code points lie in the range U+0 to U+10FFFF, excluding
127 the surrogate area. The so-called "non-character" code points are not
128 excluded because Unicode corrigendum #9 makes it clear that they should
129 not be.
130
131 Characters in the "Surrogate Area" of Unicode are reserved for use by
132 UTF-16, where they are used in pairs to encode code points with values
133 greater than 0xFFFF. The code points that are encoded by UTF-16 pairs
134 are available independently in the UTF-8 and UTF-32 encodings. (In
135 other words, the whole surrogate thing is a fudge for UTF-16 which
136 unfortunately messes up UTF-8 and UTF-32.)
137
138 In some situations, you may already know that your strings are valid,
139 and therefore want to skip these checks in order to improve perfor‐
140 mance, for example in the case of a long subject string that is being
141 scanned repeatedly. If you set the PCRE2_NO_UTF_CHECK option at com‐
142 pile time or at match time, PCRE2 assumes that the pattern or subject
143 it is given (respectively) contains only valid UTF code unit sequences.
144
145 Passing PCRE2_NO_UTF_CHECK to pcre2_compile() just disables the check
146 for the pattern; it does not also apply to subject strings. If you want
147 to disable the check for a subject string you must pass this option to
148 pcre2_match() or pcre2_dfa_match().
149
150 If you pass an invalid UTF string when PCRE2_NO_UTF_CHECK is set, the
151 result is undefined and your program may crash or loop indefinitely.
152
153 Note that setting PCRE2_NO_UTF_CHECK at compile time does not disable
154 the error that is given if an escape sequence for an invalid Unicode
155 code point is encountered in the pattern. If you want to allow escape
156 sequences such as \x{d800} (a surrogate code point) you can set the
157 PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES extra option. However, this is pos‐
158 sible only in UTF-8 and UTF-32 modes, because these values are not rep‐
159 resentable in UTF-16.
160
161 Errors in UTF-8 strings
162
163 The following negative error codes are given for invalid UTF-8 strings:
164
165 PCRE2_ERROR_UTF8_ERR1
166 PCRE2_ERROR_UTF8_ERR2
167 PCRE2_ERROR_UTF8_ERR3
168 PCRE2_ERROR_UTF8_ERR4
169 PCRE2_ERROR_UTF8_ERR5
170
171 The string ends with a truncated UTF-8 character; the code specifies
172 how many bytes are missing (1 to 5). Although RFC 3629 restricts UTF-8
173 characters to be no longer than 4 bytes, the encoding scheme (origi‐
174 nally defined by RFC 2279) allows for up to 6 bytes, and this is
175 checked first; hence the possibility of 4 or 5 missing bytes.
176
177 PCRE2_ERROR_UTF8_ERR6
178 PCRE2_ERROR_UTF8_ERR7
179 PCRE2_ERROR_UTF8_ERR8
180 PCRE2_ERROR_UTF8_ERR9
181 PCRE2_ERROR_UTF8_ERR10
182
183 The two most significant bits of the 2nd, 3rd, 4th, 5th, or 6th byte of
184 the character do not have the binary value 0b10 (that is, either the
185 most significant bit is 0, or the next bit is 1).
186
187 PCRE2_ERROR_UTF8_ERR11
188 PCRE2_ERROR_UTF8_ERR12
189
190 A character that is valid by the RFC 2279 rules is either 5 or 6 bytes
191 long; these code points are excluded by RFC 3629.
192
193 PCRE2_ERROR_UTF8_ERR13
194
195 A 4-byte character has a value greater than 0x10fff; these code points
196 are excluded by RFC 3629.
197
198 PCRE2_ERROR_UTF8_ERR14
199
200 A 3-byte character has a value in the range 0xd800 to 0xdfff; this
201 range of code points are reserved by RFC 3629 for use with UTF-16, and
202 so are excluded from UTF-8.
203
204 PCRE2_ERROR_UTF8_ERR15
205 PCRE2_ERROR_UTF8_ERR16
206 PCRE2_ERROR_UTF8_ERR17
207 PCRE2_ERROR_UTF8_ERR18
208 PCRE2_ERROR_UTF8_ERR19
209
210 A 2-, 3-, 4-, 5-, or 6-byte character is "overlong", that is, it codes
211 for a value that can be represented by fewer bytes, which is invalid.
212 For example, the two bytes 0xc0, 0xae give the value 0x2e, whose cor‐
213 rect coding uses just one byte.
214
215 PCRE2_ERROR_UTF8_ERR20
216
217 The two most significant bits of the first byte of a character have the
218 binary value 0b10 (that is, the most significant bit is 1 and the sec‐
219 ond is 0). Such a byte can only validly occur as the second or subse‐
220 quent byte of a multi-byte character.
221
222 PCRE2_ERROR_UTF8_ERR21
223
224 The first byte of a character has the value 0xfe or 0xff. These values
225 can never occur in a valid UTF-8 string.
226
227 Errors in UTF-16 strings
228
229 The following negative error codes are given for invalid UTF-16
230 strings:
231
232 PCRE2_ERROR_UTF16_ERR1 Missing low surrogate at end of string
233 PCRE2_ERROR_UTF16_ERR2 Invalid low surrogate follows high surrogate
234 PCRE2_ERROR_UTF16_ERR3 Isolated low surrogate
235
236
237 Errors in UTF-32 strings
238
239 The following negative error codes are given for invalid UTF-32
240 strings:
241
242 PCRE2_ERROR_UTF32_ERR1 Surrogate character (0xd800 to 0xdfff)
243 PCRE2_ERROR_UTF32_ERR2 Code point is greater than 0x10ffff
244
245
247
248 Philip Hazel
249 University Computing Service
250 Cambridge, England.
251
253
254 Last updated: 02 September 2018
255 Copyright (c) 1997-2018 University of Cambridge.
256
257
258
259PCRE2 10.32 02 September 2018 PCRE2UNICODE(3)