1PCRE2UNICODE(3) Library Functions Manual PCRE2UNICODE(3)
2
3
4
6 PCRE - Perl-compatible regular expressions (revised API)
7
9
10 When PCRE2 is built with Unicode support (which is the default), it has
11 knowledge of Unicode character properties and can process text strings
12 in UTF-8, UTF-16, or UTF-32 format (depending on the code unit width).
13 However, by default, PCRE2 assumes that one code unit is one character.
14 To process a pattern as a UTF string, where a character may require
15 more than one code unit, you must call pcre2_compile() with the
16 PCRE2_UTF option flag, or the pattern must start with the sequence
17 (*UTF). When either of these is the case, both the pattern and any sub‐
18 ject strings that are matched against it are treated as UTF strings
19 instead of strings of individual one-code-unit characters.
20
21 If you do not need Unicode support you can build PCRE2 without it, in
22 which case the library will be smaller.
23
25
26 When PCRE2 is built with Unicode support, the escape sequences \p{..},
27 \P{..}, and \X can be used. The Unicode properties that can be tested
28 are limited to the general category properties such as Lu for an upper
29 case letter or Nd for a decimal number, the Unicode script names such
30 as Arabic or Han, and the derived properties Any and L&. Full lists are
31 given in the pcre2pattern and pcre2syntax documentation. Only the short
32 names for properties are supported. For example, \p{L} matches a let‐
33 ter. Its Perl synonym, \p{Letter}, is not supported. Furthermore, in
34 Perl, many properties may optionally be prefixed by "Is", for compati‐
35 bility with Perl 5.6. PCRE does not support this.
36
38
39 Codepoints less than 256 can be specified in patterns by either braced
40 or unbraced hexadecimal escape sequences (for example, \x{b3} or \xb3).
41 Larger values have to use braced sequences. Unbraced octal code points
42 up to \777 are also recognized; larger ones can be coded using \o{...}.
43
44 In UTF modes, repeat quantifiers apply to complete UTF characters, not
45 to individual code units.
46
47 In UTF modes, the dot metacharacter matches one UTF character instead
48 of a single code unit.
49
50 The escape sequence \C can be used to match a single code unit in a UTF
51 mode, but its use can lead to some strange effects because it breaks up
52 multi-unit characters (see the description of \C in the pcre2pattern
53 documentation).
54
55 The use of \C is not supported by the alternative matching function
56 pcre2_dfa_match() when in UTF-8 or UTF-16 mode, that is, when a charac‐
57 ter may consist of more than one code unit. The use of \C in these
58 modes provokes a match-time error. Also, the JIT optimization does not
59 support \C in these modes. If JIT optimization is requested for a UTF-8
60 or UTF-16 pattern that contains \C, it will not succeed, and so when
61 pcre2_match() is called, the matching will be carried out by the normal
62 interpretive function.
63
64 The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly test
65 characters of any code value, but, by default, the characters that
66 PCRE2 recognizes as digits, spaces, or word characters remain the same
67 set as in non-UTF mode, all with code points less than 256. This
68 remains true even when PCRE2 is built to include Unicode support,
69 because to do otherwise would slow down matching in many common cases.
70 Note that this also applies to \b and \B, because they are defined in
71 terms of \w and \W. If you want to test for a wider sense of, say,
72 "digit", you can use explicit Unicode property tests such as \p{Nd}.
73 Alternatively, if you set the PCRE2_UCP option, the way that the char‐
74 acter escapes work is changed so that Unicode properties are used to
75 determine which characters match. There are more details in the section
76 on generic character types in the pcre2pattern documentation.
77
78 Similarly, characters that match the POSIX named character classes are
79 all low-valued characters, unless the PCRE2_UCP option is set.
80
81 However, the special horizontal and vertical white space matching
82 escapes (\h, \H, \v, and \V) do match all the appropriate Unicode char‐
83 acters, whether or not PCRE2_UCP is set.
84
85 Case-insensitive matching in UTF mode makes use of Unicode properties.
86 A few Unicode characters such as Greek sigma have more than two code‐
87 points that are case-equivalent, and these are treated as such.
88
90
91 When the PCRE2_UTF option is set, the strings passed as patterns and
92 subjects are (by default) checked for validity on entry to the relevant
93 functions. If an invalid UTF string is passed, an negative error code
94 is returned. The code unit offset to the offending character can be
95 extracted from the match data block by calling pcre2_get_startchar(),
96 which is used for this purpose after a UTF error.
97
98 UTF-16 and UTF-32 strings can indicate their endianness by special code
99 knows as a byte-order mark (BOM). The PCRE2 functions do not handle
100 this, expecting strings to be in host byte order.
101
102 A UTF string is checked before any other processing takes place. In the
103 case of pcre2_match() and pcre2_dfa_match() calls with a non-zero
104 starting offset, the check is applied only to that part of the subject
105 that could be inspected during matching, and there is a check that the
106 starting offset points to the first code unit of a character or to the
107 end of the subject. If there are no lookbehind assertions in the pat‐
108 tern, the check starts at the starting offset. Otherwise, it starts at
109 the length of the longest lookbehind before the starting offset, or at
110 the start of the subject if there are not that many characters before
111 the starting offset. Note that the sequences \b and \B are one-charac‐
112 ter lookbehinds.
113
114 In addition to checking the format of the string, there is a check to
115 ensure that all code points lie in the range U+0 to U+10FFFF, excluding
116 the surrogate area. The so-called "non-character" code points are not
117 excluded because Unicode corrigendum #9 makes it clear that they should
118 not be.
119
120 Characters in the "Surrogate Area" of Unicode are reserved for use by
121 UTF-16, where they are used in pairs to encode code points with values
122 greater than 0xFFFF. The code points that are encoded by UTF-16 pairs
123 are available independently in the UTF-8 and UTF-32 encodings. (In
124 other words, the whole surrogate thing is a fudge for UTF-16 which
125 unfortunately messes up UTF-8 and UTF-32.)
126
127 In some situations, you may already know that your strings are valid,
128 and therefore want to skip these checks in order to improve perfor‐
129 mance, for example in the case of a long subject string that is being
130 scanned repeatedly. If you set the PCRE2_NO_UTF_CHECK option at com‐
131 pile time or at match time, PCRE2 assumes that the pattern or subject
132 it is given (respectively) contains only valid UTF code unit sequences.
133
134 Passing PCRE2_NO_UTF_CHECK to pcre2_compile() just disables the check
135 for the pattern; it does not also apply to subject strings. If you want
136 to disable the check for a subject string you must pass this option to
137 pcre2_match() or pcre2_dfa_match().
138
139 If you pass an invalid UTF string when PCRE2_NO_UTF_CHECK is set, the
140 result is undefined and your program may crash or loop indefinitely.
141
142 Errors in UTF-8 strings
143
144 The following negative error codes are given for invalid UTF-8 strings:
145
146 PCRE2_ERROR_UTF8_ERR1
147 PCRE2_ERROR_UTF8_ERR2
148 PCRE2_ERROR_UTF8_ERR3
149 PCRE2_ERROR_UTF8_ERR4
150 PCRE2_ERROR_UTF8_ERR5
151
152 The string ends with a truncated UTF-8 character; the code specifies
153 how many bytes are missing (1 to 5). Although RFC 3629 restricts UTF-8
154 characters to be no longer than 4 bytes, the encoding scheme (origi‐
155 nally defined by RFC 2279) allows for up to 6 bytes, and this is
156 checked first; hence the possibility of 4 or 5 missing bytes.
157
158 PCRE2_ERROR_UTF8_ERR6
159 PCRE2_ERROR_UTF8_ERR7
160 PCRE2_ERROR_UTF8_ERR8
161 PCRE2_ERROR_UTF8_ERR9
162 PCRE2_ERROR_UTF8_ERR10
163
164 The two most significant bits of the 2nd, 3rd, 4th, 5th, or 6th byte of
165 the character do not have the binary value 0b10 (that is, either the
166 most significant bit is 0, or the next bit is 1).
167
168 PCRE2_ERROR_UTF8_ERR11
169 PCRE2_ERROR_UTF8_ERR12
170
171 A character that is valid by the RFC 2279 rules is either 5 or 6 bytes
172 long; these code points are excluded by RFC 3629.
173
174 PCRE2_ERROR_UTF8_ERR13
175
176 A 4-byte character has a value greater than 0x10fff; these code points
177 are excluded by RFC 3629.
178
179 PCRE2_ERROR_UTF8_ERR14
180
181 A 3-byte character has a value in the range 0xd800 to 0xdfff; this
182 range of code points are reserved by RFC 3629 for use with UTF-16, and
183 so are excluded from UTF-8.
184
185 PCRE2_ERROR_UTF8_ERR15
186 PCRE2_ERROR_UTF8_ERR16
187 PCRE2_ERROR_UTF8_ERR17
188 PCRE2_ERROR_UTF8_ERR18
189 PCRE2_ERROR_UTF8_ERR19
190
191 A 2-, 3-, 4-, 5-, or 6-byte character is "overlong", that is, it codes
192 for a value that can be represented by fewer bytes, which is invalid.
193 For example, the two bytes 0xc0, 0xae give the value 0x2e, whose cor‐
194 rect coding uses just one byte.
195
196 PCRE2_ERROR_UTF8_ERR20
197
198 The two most significant bits of the first byte of a character have the
199 binary value 0b10 (that is, the most significant bit is 1 and the sec‐
200 ond is 0). Such a byte can only validly occur as the second or subse‐
201 quent byte of a multi-byte character.
202
203 PCRE2_ERROR_UTF8_ERR21
204
205 The first byte of a character has the value 0xfe or 0xff. These values
206 can never occur in a valid UTF-8 string.
207
208 Errors in UTF-16 strings
209
210 The following negative error codes are given for invalid UTF-16
211 strings:
212
213 PCRE2_ERROR_UTF16_ERR1 Missing low surrogate at end of string
214 PCRE2_ERROR_UTF16_ERR2 Invalid low surrogate follows high surrogate
215 PCRE2_ERROR_UTF16_ERR3 Isolated low surrogate
216
217
218 Errors in UTF-32 strings
219
220 The following negative error codes are given for invalid UTF-32
221 strings:
222
223 PCRE2_ERROR_UTF32_ERR1 Surrogate character (0xd800 to 0xdfff)
224 PCRE2_ERROR_UTF32_ERR2 Code point is greater than 0x10ffff
225
226
228
229 Philip Hazel
230 University Computing Service
231 Cambridge, England.
232
234
235 Last updated: 03 July 2016
236 Copyright (c) 1997-2016 University of Cambridge.
237
238
239
240PCRE2 10.22 03 July 2016 PCRE2UNICODE(3)