blob: 96fa62e86ad8eb0867a946c8d39a870173a1627a [file] [log] [blame]
Tristan Matthews04616462013-11-14 16:09:34 -05001<html>
2<head>
3<title>pcreunicode specification</title>
4</head>
5<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6<h1>pcreunicode man page</h1>
7<p>
8Return to the <a href="index.html">PCRE index page</a>.
9</p>
10<p>
11This page is part of the PCRE HTML documentation. It was generated automatically
12from the original man page. If there is any nonsense in it, please consult the
13man page, in case the conversion went wrong.
14<br>
15<br><b>
16UTF-8 AND UNICODE PROPERTY SUPPORT
17</b><br>
18<P>
19In order process UTF-8 strings, you must build PCRE to include UTF-8 support in
20the code, and, in addition, you must call
21<a href="pcre_compile.html"><b>pcre_compile()</b></a>
22with the PCRE_UTF8 option flag, or the pattern must start with the sequence
23(*UTF8). When either of these is the case, both the pattern and any subject
24strings that are matched against it are treated as UTF-8 strings instead of
25strings of 1-byte characters. PCRE does not support any other formats (in
26particular, it does not support UTF-16).
27</P>
28<P>
29If you compile PCRE with UTF-8 support, but do not use it at run time, the
30library will be a bit bigger, but the additional run time overhead is limited
31to testing the PCRE_UTF8 flag occasionally, so should not be very big.
32</P>
33<P>
34If PCRE is built with Unicode character property support (which implies UTF-8
35support), the escape sequences \p{..}, \P{..}, and \X are supported.
36The available properties that can be tested are limited to the general
37category properties such as Lu for an upper case letter or Nd for a decimal
38number, the Unicode script names such as Arabic or Han, and the derived
39properties Any and L&. A full list is given in the
40<a href="pcrepattern.html"><b>pcrepattern</b></a>
41documentation. Only the short names for properties are supported. For example,
42\p{L} matches a letter. Its Perl synonym, \p{Letter}, is not supported.
43Furthermore, in Perl, many properties may optionally be prefixed by "Is", for
44compatibility with Perl 5.6. PCRE does not support this.
45<a name="utf8strings"></a></P>
46<br><b>
47Validity of UTF-8 strings
48</b><br>
49<P>
50When you set the PCRE_UTF8 flag, the strings passed as patterns and subjects
51are (by default) checked for validity on entry to the relevant functions. From
52release 7.3 of PCRE, the check is according the rules of RFC 3629, which are
53themselves derived from the Unicode specification. Earlier releases of PCRE
54followed the rules of RFC 2279, which allows the full range of 31-bit values (0
55to 0x7FFFFFFF). The current check allows only values in the range U+0 to
56U+10FFFF, excluding U+D800 to U+DFFF.
57</P>
58<P>
59The excluded code points are the "Low Surrogate Area" of Unicode, of which the
60Unicode Standard says this: "The Low Surrogate Area does not contain any
61character assignments, consequently no character code charts or namelists are
62provided for this area. Surrogates are reserved for use with UTF-16 and then
63must be used in pairs." The code points that are encoded by UTF-16 pairs are
64available as independent code points in the UTF-8 encoding. (In other words,
65the whole surrogate thing is a fudge for UTF-16 which unfortunately messes up
66UTF-8.)
67</P>
68<P>
69If an invalid UTF-8 string is passed to PCRE, an error return is given. At
70compile time, the only additional information is the offset to the first byte
71of the failing character. The runtime functions <b>pcre_exec()</b> and
72<b>pcre_dfa_exec()</b> also pass back this information, as well as a more
73detailed reason code if the caller has provided memory in which to do this.
74</P>
75<P>
76In some situations, you may already know that your strings are valid, and
77therefore want to skip these checks in order to improve performance. If you set
78the PCRE_NO_UTF8_CHECK flag at compile time or at run time, PCRE assumes that
79the pattern or subject it is given (respectively) contains only valid UTF-8
80codes. In this case, it does not diagnose an invalid UTF-8 string.
81</P>
82<P>
83If you pass an invalid UTF-8 string when PCRE_NO_UTF8_CHECK is set, what
84happens depends on why the string is invalid. If the string conforms to the
85"old" definition of UTF-8 (RFC 2279), it is processed as a string of characters
86in the range 0 to 0x7FFFFFFF by <b>pcre_dfa_exec()</b> and the interpreted
87version of <b>pcre_exec()</b>. In other words, apart from the initial validity
88test, these functions (when in UTF-8 mode) handle strings according to the more
89liberal rules of RFC 2279. However, the just-in-time (JIT) optimization for
90<b>pcre_exec()</b> supports only RFC 3629. If you are using JIT optimization, or
91if the string does not even conform to RFC 2279, the result is undefined. Your
92program may crash.
93</P>
94<P>
95If you want to process strings of values in the full range 0 to 0x7FFFFFFF,
96encoded in a UTF-8-like manner as per the old RFC, you can set
97PCRE_NO_UTF8_CHECK to bypass the more restrictive test. However, in this
98situation, you will have to apply your own validity check, and avoid the use of
99JIT optimization.
100</P>
101<br><b>
102General comments about UTF-8 mode
103</b><br>
104<P>
1051. An unbraced hexadecimal escape sequence (such as \xb3) matches a two-byte
106UTF-8 character if the value is greater than 127.
107</P>
108<P>
1092. Octal numbers up to \777 are recognized, and match two-byte UTF-8
110characters for values greater than \177.
111</P>
112<P>
1133. Repeat quantifiers apply to complete UTF-8 characters, not to individual
114bytes, for example: \x{100}{3}.
115</P>
116<P>
1174. The dot metacharacter matches one UTF-8 character instead of a single byte.
118</P>
119<P>
1205. The escape sequence \C can be used to match a single byte in UTF-8 mode,
121but its use can lead to some strange effects because it breaks up multibyte
122characters (see the description of \C in the
123<a href="pcrepattern.html"><b>pcrepattern</b></a>
124documentation). The use of \C is not supported in the alternative matching
125function <b>pcre_dfa_exec()</b>, nor is it supported in UTF-8 mode by the JIT
126optimization of <b>pcre_exec()</b>. If JIT optimization is requested for a UTF-8
127pattern that contains \C, it will not succeed, and so the matching will be
128carried out by the normal interpretive function.
129</P>
130<P>
1316. The character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly
132test characters of any code value, but, by default, the characters that PCRE
133recognizes as digits, spaces, or word characters remain the same set as before,
134all with values less than 256. This remains true even when PCRE is built to
135include Unicode property support, because to do otherwise would slow down PCRE
136in many common cases. Note in particular that this applies to \b and \B,
137because they are defined in terms of \w and \W. If you really want to test
138for a wider sense of, say, "digit", you can use explicit Unicode property tests
139such as \p{Nd}. Alternatively, if you set the PCRE_UCP option, the way that
140the character escapes work is changed so that Unicode properties are used to
141determine which characters match. There are more details in the section on
142<a href="pcrepattern.html#genericchartypes">generic character types</a>
143in the
144<a href="pcrepattern.html"><b>pcrepattern</b></a>
145documentation.
146</P>
147<P>
1487. Similarly, characters that match the POSIX named character classes are all
149low-valued characters, unless the PCRE_UCP option is set.
150</P>
151<P>
1528. However, the horizontal and vertical whitespace matching escapes (\h, \H,
153\v, and \V) do match all the appropriate Unicode characters, whether or not
154PCRE_UCP is set.
155</P>
156<P>
1579. Case-insensitive matching applies only to characters whose values are less
158than 128, unless PCRE is built with Unicode property support. Even when Unicode
159property support is available, PCRE still uses its own character tables when
160checking the case of low-valued characters, so as not to degrade performance.
161The Unicode property information is used only for characters with higher
162values. Furthermore, PCRE supports case-insensitive matching only when there is
163a one-to-one mapping between a letter's cases. There are a small number of
164many-to-one mappings in Unicode; these are not supported by PCRE.
165</P>
166<br><b>
167AUTHOR
168</b><br>
169<P>
170Philip Hazel
171<br>
172University Computing Service
173<br>
174Cambridge CB2 3QH, England.
175<br>
176</P>
177<br><b>
178REVISION
179</b><br>
180<P>
181Last updated: 19 October 2011
182<br>
183Copyright &copy; 1997-2011 University of Cambridge.
184<br>
185<p>
186Return to the <a href="index.html">PCRE index page</a>.
187</p>