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