1PCRE2UNICODE(3) Library Functions Manual PCRE2UNICODE(3)
2
3
4
6 PCRE - Perl-compatible regular expressions (revised API)
7
9
10 PCRE2 is normally built with Unicode support, though if you do not need
11 it, you can build it without, in which case the library will be
12 smaller. With Unicode support, PCRE2 has knowledge of Unicode character
13 properties and can process strings of text in UTF-8, UTF-16, and UTF-32
14 format (depending on the code unit width), but this is not the default.
15 Unless specifically requested, PCRE2 treats each code unit in a string
16 as one character.
17
18 There are two ways of telling PCRE2 to switch to UTF mode, where char‐
19 acters may consist of more than one code unit and the range of values
20 is constrained. The program can call pcre2_compile() with the PCRE2_UTF
21 option, or the pattern may start with the sequence (*UTF). However,
22 the latter facility can be locked out by the PCRE2_NEVER_UTF option.
23 That is, the programmer can prevent the supplier of the pattern from
24 switching to UTF mode.
25
26 Note that the PCRE2_MATCH_INVALID_UTF option (see below) forces
27 PCRE2_UTF to be set.
28
29 In UTF mode, both the pattern and any subject strings that are matched
30 against it are treated as UTF strings instead of strings of individual
31 one-code-unit characters. There are also some other changes to the way
32 characters are handled, as documented below.
33
35
36 When PCRE2 is built with Unicode support, the escape sequences \p{..},
37 \P{..}, and \X can be used. This is not dependent on the PCRE2_UTF set‐
38 ting. The Unicode properties that can be tested are a subset of those
39 that Perl supports. Currently they are limited to the general category
40 properties such as Lu for an upper case letter or Nd for a decimal num‐
41 ber, the Unicode script names such as Arabic or Han, Bidi_Class,
42 Bidi_Control, and the derived properties Any and LC (synonym L&). Full
43 lists are given in the pcre2pattern and pcre2syntax documentation. In
44 general, only the short names for properties are supported. For exam‐
45 ple, \p{L} matches a letter. Its longer synonym, \p{Letter}, is not
46 supported. Furthermore, in Perl, many properties may optionally be pre‐
47 fixed by "Is", for compatibility with Perl 5.6. PCRE2 does not support
48 this.
49
51
52 Code points less than 256 can be specified in patterns by either braced
53 or unbraced hexadecimal escape sequences (for example, \x{b3} or \xb3).
54 Larger values have to use braced sequences. Unbraced octal code points
55 up to \777 are also recognized; larger ones can be coded using \o{...}.
56
57 The escape sequence \N{U+<hex digits>} is recognized as another way of
58 specifying a Unicode character by code point in a UTF mode. It is not
59 allowed in non-UTF mode.
60
61 In UTF mode, repeat quantifiers apply to complete UTF characters, not
62 to individual code units.
63
64 In UTF mode, the dot metacharacter matches one UTF character instead of
65 a single code unit.
66
67 In UTF mode, capture group names are not restricted to ASCII, and may
68 contain any Unicode letters and decimal digits, as well as underscore.
69
70 The escape sequence \C can be used to match a single code unit in UTF
71 mode, but its use can lead to some strange effects because it breaks up
72 multi-unit characters (see the description of \C in the pcre2pattern
73 documentation). For this reason, there is a build-time option that dis‐
74 ables support for \C completely. There is also a less draconian com‐
75 pile-time option for locking out the use of \C when a pattern is com‐
76 piled.
77
78 The use of \C is not supported by the alternative matching function
79 pcre2_dfa_match() when in UTF-8 or UTF-16 mode, that is, when a charac‐
80 ter may consist of more than one code unit. The use of \C in these
81 modes provokes a match-time error. Also, the JIT optimization does not
82 support \C in these modes. If JIT optimization is requested for a UTF-8
83 or UTF-16 pattern that contains \C, it will not succeed, and so when
84 pcre2_match() is called, the matching will be carried out by the inter‐
85 pretive function.
86
87 The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly test
88 characters of any code value, but, by default, the characters that
89 PCRE2 recognizes as digits, spaces, or word characters remain the same
90 set as in non-UTF mode, all with code points less than 256. This re‐
91 mains true even when PCRE2 is built to include Unicode support, because
92 to do otherwise would slow down matching in many common cases. Note
93 that this also applies to \b and \B, because they are defined in terms
94 of \w and \W. If you want to test for a wider sense of, say, "digit",
95 you can use explicit Unicode property tests such as \p{Nd}. Alterna‐
96 tively, if you set the PCRE2_UCP option, the way that the character es‐
97 capes work is changed so that Unicode properties are used to determine
98 which characters match. There are more details in the section on
99 generic character types in the pcre2pattern documentation.
100
101 Similarly, characters that match the POSIX named character classes are
102 all low-valued characters, unless the PCRE2_UCP option is set.
103
104 However, the special horizontal and vertical white space matching es‐
105 capes (\h, \H, \v, and \V) do match all the appropriate Unicode charac‐
106 ters, whether or not PCRE2_UCP is set.
107
109
110 If either PCRE2_UTF or PCRE2_UCP is set, upper/lower case processing
111 makes use of Unicode properties except for characters whose code points
112 are less than 128 and that have at most two case-equivalent values. For
113 these, a direct table lookup is used for speed. A few Unicode charac‐
114 ters such as Greek sigma have more than two code points that are case-
115 equivalent, and these are treated specially. Setting PCRE2_UCP without
116 PCRE2_UTF allows Unicode-style case processing for non-UTF character
117 encodings such as UCS-2.
118
120
121 The pattern constructs (*script_run:...) and (*atomic_script_run:...),
122 with synonyms (*sr:...) and (*asr:...), verify that the string matched
123 within the parentheses is a script run. In concept, a script run is a
124 sequence of characters that are all from the same Unicode script. How‐
125 ever, because some scripts are commonly used together, and because some
126 diacritical and other marks are used with multiple scripts, it is not
127 that simple.
128
129 Every Unicode character has a Script property, mostly with a value cor‐
130 responding to the name of a script, such as Latin, Greek, or Cyrillic.
131 There are also three special values:
132
133 "Unknown" is used for code points that have not been assigned, and also
134 for the surrogate code points. In the PCRE2 32-bit library, characters
135 whose code points are greater than the Unicode maximum (U+10FFFF),
136 which are accessible only in non-UTF mode, are assigned the Unknown
137 script.
138
139 "Common" is used for characters that are used with many scripts. These
140 include punctuation, emoji, mathematical, musical, and currency sym‐
141 bols, and the ASCII digits 0 to 9.
142
143 "Inherited" is used for characters such as diacritical marks that mod‐
144 ify a previous character. These are considered to take on the script of
145 the character that they modify.
146
147 Some Inherited characters are used with many scripts, but many of them
148 are only normally used with a small number of scripts. For example,
149 U+102E0 (Coptic Epact thousands mark) is used only with Arabic and Cop‐
150 tic. In order to make it possible to check this, a Unicode property
151 called Script Extension exists. Its value is a list of scripts that ap‐
152 ply to the character. For the majority of characters, the list contains
153 just one script, the same one as the Script property. However, for
154 characters such as U+102E0 more than one Script is listed. There are
155 also some Common characters that have a single, non-Common script in
156 their Script Extension list.
157
158 The next section describes the basic rules for deciding whether a given
159 string of characters is a script run. Note, however, that there are
160 some special cases involving the Chinese Han script, and an additional
161 constraint for decimal digits. These are covered in subsequent sec‐
162 tions.
163
164 Basic script run rules
165
166 A string that is less than two characters long is a script run. This is
167 the only case in which an Unknown character can be part of a script
168 run. Longer strings are checked using only the Script Extensions prop‐
169 erty, not the basic Script property.
170
171 If a character's Script Extension property is the single value "Inher‐
172 ited", it is always accepted as part of a script run. This is also true
173 for the property "Common", subject to the checking of decimal digits
174 described below. All the remaining characters in a script run must have
175 at least one script in common in their Script Extension lists. In set-
176 theoretic terminology, the intersection of all the sets of scripts must
177 not be empty.
178
179 A simple example is an Internet name such as "google.com". The letters
180 are all in the Latin script, and the dot is Common, so this string is a
181 script run. However, the Cyrillic letter "o" looks exactly the same as
182 the Latin "o"; a string that looks the same, but with Cyrillic "o"s is
183 not a script run.
184
185 More interesting examples involve characters with more than one script
186 in their Script Extension. Consider the following characters:
187
188 U+060C Arabic comma
189 U+06D4 Arabic full stop
190
191 The first has the Script Extension list Arabic, Hanifi Rohingya, Syr‐
192 iac, and Thaana; the second has just Arabic and Hanifi Rohingya. Both
193 of them could appear in script runs of either Arabic or Hanifi Ro‐
194 hingya. The first could also appear in Syriac or Thaana script runs,
195 but the second could not.
196
197 The Chinese Han script
198
199 The Chinese Han script is commonly used in conjunction with other
200 scripts for writing certain languages. Japanese uses the Hiragana and
201 Katakana scripts together with Han; Korean uses Hangul and Han; Tai‐
202 wanese Mandarin uses Bopomofo and Han. These three combinations are
203 treated as special cases when checking script runs and are, in effect,
204 "virtual scripts". Thus, a script run may contain a mixture of Hira‐
205 gana, Katakana, and Han, or a mixture of Hangul and Han, or a mixture
206 of Bopomofo and Han, but not, for example, a mixture of Hangul and
207 Bopomofo and Han. PCRE2 (like Perl) follows Unicode's Technical Stan‐
208 dard 39 ("Unicode Security Mechanisms", http://unicode.org/re‐
209 ports/tr39/) in allowing such mixtures.
210
211 Decimal digits
212
213 Unicode contains many sets of 10 decimal digits in different scripts,
214 and some scripts (including the Common script) contain more than one
215 set. Some of these decimal digits them are visually indistinguishable
216 from the common ASCII digits. In addition to the script checking de‐
217 scribed above, if a script run contains any decimal digits, they must
218 all come from the same set of 10 adjacent characters.
219
221
222 When the PCRE2_UTF option is set, the strings passed as patterns and
223 subjects are (by default) checked for validity on entry to the relevant
224 functions. If an invalid UTF string is passed, a negative error code is
225 returned. The code unit offset to the offending character can be ex‐
226 tracted from the match data block by calling pcre2_get_startchar(),
227 which is used for this purpose after a UTF error.
228
229 In some situations, you may already know that your strings are valid,
230 and therefore want to skip these checks in order to improve perfor‐
231 mance, for example in the case of a long subject string that is being
232 scanned repeatedly. If you set the PCRE2_NO_UTF_CHECK option at com‐
233 pile time or at match time, PCRE2 assumes that the pattern or subject
234 it is given (respectively) contains only valid UTF code unit sequences.
235
236 If you pass an invalid UTF string when PCRE2_NO_UTF_CHECK is set, the
237 result is undefined and your program may crash or loop indefinitely or
238 give incorrect results. There is, however, one mode of matching that
239 can handle invalid UTF subject strings. This is enabled by passing
240 PCRE2_MATCH_INVALID_UTF to pcre2_compile() and is discussed below in
241 the next section. The rest of this section covers the case when
242 PCRE2_MATCH_INVALID_UTF is not set.
243
244 Passing PCRE2_NO_UTF_CHECK to pcre2_compile() just disables the UTF
245 check for the pattern; it does not also apply to subject strings. If
246 you want to disable the check for a subject string you must pass this
247 same option to pcre2_match() or pcre2_dfa_match().
248
249 UTF-16 and UTF-32 strings can indicate their endianness by special code
250 knows as a byte-order mark (BOM). The PCRE2 functions do not handle
251 this, expecting strings to be in host byte order.
252
253 Unless PCRE2_NO_UTF_CHECK is set, a UTF string is checked before any
254 other processing takes place. In the case of pcre2_match() and
255 pcre2_dfa_match() calls with a non-zero starting offset, the check is
256 applied only to that part of the subject that could be inspected during
257 matching, and there is a check that the starting offset points to the
258 first code unit of a character or to the end of the subject. If there
259 are no lookbehind assertions in the pattern, the check starts at the
260 starting offset. Otherwise, it starts at the length of the longest
261 lookbehind before the starting offset, or at the start of the subject
262 if there are not that many characters before the starting offset. Note
263 that the sequences \b and \B are one-character lookbehinds.
264
265 In addition to checking the format of the string, there is a check to
266 ensure that all code points lie in the range U+0 to U+10FFFF, excluding
267 the surrogate area. The so-called "non-character" code points are not
268 excluded because Unicode corrigendum #9 makes it clear that they should
269 not be.
270
271 Characters in the "Surrogate Area" of Unicode are reserved for use by
272 UTF-16, where they are used in pairs to encode code points with values
273 greater than 0xFFFF. The code points that are encoded by UTF-16 pairs
274 are available independently in the UTF-8 and UTF-32 encodings. (In
275 other words, the whole surrogate thing is a fudge for UTF-16 which un‐
276 fortunately messes up UTF-8 and UTF-32.)
277
278 Setting PCRE2_NO_UTF_CHECK at compile time does not disable the error
279 that is given if an escape sequence for an invalid Unicode code point
280 is encountered in the pattern. If you want to allow escape sequences
281 such as \x{d800} (a surrogate code point) you can set the PCRE2_EX‐
282 TRA_ALLOW_SURROGATE_ESCAPES extra option. However, this is possible
283 only in UTF-8 and UTF-32 modes, because these values are not repre‐
284 sentable in UTF-16.
285
286 Errors in UTF-8 strings
287
288 The following negative error codes are given for invalid UTF-8 strings:
289
290 PCRE2_ERROR_UTF8_ERR1
291 PCRE2_ERROR_UTF8_ERR2
292 PCRE2_ERROR_UTF8_ERR3
293 PCRE2_ERROR_UTF8_ERR4
294 PCRE2_ERROR_UTF8_ERR5
295
296 The string ends with a truncated UTF-8 character; the code specifies
297 how many bytes are missing (1 to 5). Although RFC 3629 restricts UTF-8
298 characters to be no longer than 4 bytes, the encoding scheme (origi‐
299 nally defined by RFC 2279) allows for up to 6 bytes, and this is
300 checked first; hence the possibility of 4 or 5 missing bytes.
301
302 PCRE2_ERROR_UTF8_ERR6
303 PCRE2_ERROR_UTF8_ERR7
304 PCRE2_ERROR_UTF8_ERR8
305 PCRE2_ERROR_UTF8_ERR9
306 PCRE2_ERROR_UTF8_ERR10
307
308 The two most significant bits of the 2nd, 3rd, 4th, 5th, or 6th byte of
309 the character do not have the binary value 0b10 (that is, either the
310 most significant bit is 0, or the next bit is 1).
311
312 PCRE2_ERROR_UTF8_ERR11
313 PCRE2_ERROR_UTF8_ERR12
314
315 A character that is valid by the RFC 2279 rules is either 5 or 6 bytes
316 long; these code points are excluded by RFC 3629.
317
318 PCRE2_ERROR_UTF8_ERR13
319
320 A 4-byte character has a value greater than 0x10ffff; these code points
321 are excluded by RFC 3629.
322
323 PCRE2_ERROR_UTF8_ERR14
324
325 A 3-byte character has a value in the range 0xd800 to 0xdfff; this
326 range of code points are reserved by RFC 3629 for use with UTF-16, and
327 so are excluded from UTF-8.
328
329 PCRE2_ERROR_UTF8_ERR15
330 PCRE2_ERROR_UTF8_ERR16
331 PCRE2_ERROR_UTF8_ERR17
332 PCRE2_ERROR_UTF8_ERR18
333 PCRE2_ERROR_UTF8_ERR19
334
335 A 2-, 3-, 4-, 5-, or 6-byte character is "overlong", that is, it codes
336 for a value that can be represented by fewer bytes, which is invalid.
337 For example, the two bytes 0xc0, 0xae give the value 0x2e, whose cor‐
338 rect coding uses just one byte.
339
340 PCRE2_ERROR_UTF8_ERR20
341
342 The two most significant bits of the first byte of a character have the
343 binary value 0b10 (that is, the most significant bit is 1 and the sec‐
344 ond is 0). Such a byte can only validly occur as the second or subse‐
345 quent byte of a multi-byte character.
346
347 PCRE2_ERROR_UTF8_ERR21
348
349 The first byte of a character has the value 0xfe or 0xff. These values
350 can never occur in a valid UTF-8 string.
351
352 Errors in UTF-16 strings
353
354 The following negative error codes are given for invalid UTF-16
355 strings:
356
357 PCRE2_ERROR_UTF16_ERR1 Missing low surrogate at end of string
358 PCRE2_ERROR_UTF16_ERR2 Invalid low surrogate follows high surrogate
359 PCRE2_ERROR_UTF16_ERR3 Isolated low surrogate
360
361
362 Errors in UTF-32 strings
363
364 The following negative error codes are given for invalid UTF-32
365 strings:
366
367 PCRE2_ERROR_UTF32_ERR1 Surrogate character (0xd800 to 0xdfff)
368 PCRE2_ERROR_UTF32_ERR2 Code point is greater than 0x10ffff
369
370
372
373 You can run pattern matches on subject strings that may contain invalid
374 UTF sequences if you call pcre2_compile() with the PCRE2_MATCH_IN‐
375 VALID_UTF option. This is supported by pcre2_match(), including JIT
376 matching, but not by pcre2_dfa_match(). When PCRE2_MATCH_INVALID_UTF is
377 set, it forces PCRE2_UTF to be set as well. Note, however, that the
378 pattern itself must be a valid UTF string.
379
380 Setting PCRE2_MATCH_INVALID_UTF does not affect what pcre2_compile()
381 generates, but if pcre2_jit_compile() is subsequently called, it does
382 generate different code. If JIT is not used, the option affects the be‐
383 haviour of the interpretive code in pcre2_match(). When PCRE2_MATCH_IN‐
384 VALID_UTF is set at compile time, PCRE2_NO_UTF_CHECK is ignored at
385 match time.
386
387 In this mode, an invalid code unit sequence in the subject never
388 matches any pattern item. It does not match dot, it does not match
389 \p{Any}, it does not even match negative items such as [^X]. A lookbe‐
390 hind assertion fails if it encounters an invalid sequence while moving
391 the current point backwards. In other words, an invalid UTF code unit
392 sequence acts as a barrier which no match can cross.
393
394 You can also think of this as the subject being split up into fragments
395 of valid UTF, delimited internally by invalid code unit sequences. The
396 pattern is matched fragment by fragment. The result of a successful
397 match, however, is given as code unit offsets in the entire subject
398 string in the usual way. There are a few points to consider:
399
400 The internal boundaries are not interpreted as the beginnings or ends
401 of lines and so do not match circumflex or dollar characters in the
402 pattern.
403
404 If pcre2_match() is called with an offset that points to an invalid
405 UTF-sequence, that sequence is skipped, and the match starts at the
406 next valid UTF character, or the end of the subject.
407
408 At internal fragment boundaries, \b and \B behave in the same way as at
409 the beginning and end of the subject. For example, a sequence such as
410 \bWORD\b would match an instance of WORD that is surrounded by invalid
411 UTF code units.
412
413 Using PCRE2_MATCH_INVALID_UTF, an application can run matches on arbi‐
414 trary data, knowing that any matched strings that are returned are
415 valid UTF. This can be useful when searching for UTF text in executable
416 or other binary files.
417
419
420 Philip Hazel
421 Retired from University Computing Service
422 Cambridge, England.
423
425
426 Last updated: 22 December 2021
427 Copyright (c) 1997-2021 University of Cambridge.
428
429
430
431PCRE2 10.40 22 December 2021 PCRE2UNICODE(3)