pcre2unicode.3 20 KB

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