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