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