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