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