PCREUNICODE(3)

πŸ’– PCREUNICODE(3) πŸ’–

okay bestie, welcome to the PCREUNICODE(3) page from the Library Functions Manual β€” this is the one about Unicode and all the UTF flavours. grab a boba, we're going in πŸ§‹βœ¨


🌸 NAME 🌸

PCRE β€” Perl-compatible regular expressions. that's it, that's the name, iconic behavior only πŸ’…


πŸ¦‹ UTF-8, UTF-16, UTF-32, AND UNICODE PROPERTY SUPPORT πŸ¦‹

so listen. PCRE doesn't just do UTF-8 β€” it also does UTF-16 (since release 8.30) and UTF-32 (since release 8.32), and it pulls this off with two extra libraries. and the tea is: those libraries can be built as well as the 8-bit library, or instead of it. your choice, your build, your rules πŸ‘‘


✨ UTF-8 SUPPORT ✨

to actually process UTF-8 strings you gotta do two things, no skipping:

  • build PCRE's 8-bit library with UTF support, and
  • call pcre_compile() with the PCRE_UTF8 option flag β€” or make the pattern start with (*UTF8) or (*UTF).

once either of those is true? both the pattern and every subject string you match against it get treated as UTF-8 strings, instead of strings of individual 1-byte characters. glow up complete πŸ’–


🌈 UTF-16 AND UTF-32 SUPPORT 🌈

same energy, different bit-width. to process UTF-16 or UTF-32 strings you must:

  • build PCRE's 16-bit or 32-bit library with UTF support, and
  • call pcre16_compile() or pcre32_compile() with the PCRE_UTF16 or PCRE_UTF32 option flag, as appropriate.

or, alternative route: the pattern starts with (*UTF16), (*UTF32), as appropriate β€” or (*UTF), which works with either library. when UTF mode is set, both the pattern and any subject strings matched against it are treated as UTF-16 or UTF-32 strings instead of strings of individual 16-bit or 32-bit characters. periodt ✨


πŸ’… UTF SUPPORT OVERHEAD πŸ’…

if you compile PCRE with UTF support but never actually use it at run time, the library is a bit bigger. that's it though. the extra run time overhead is limited to occasionally testing the PCRE_UTF[8|16|32] flag, so it should not be very big. lowkey negligible, ngl 🫢


πŸ‘‘ UNICODE PROPERTY SUPPORT πŸ‘‘

if PCRE is built with Unicode character property support (which implies UTF support, iykyk), then you unlock the escape sequences \p{..}, \P{..}, and \X. πŸŽ€

what properties can you test? it's limited to:

  • the general category properties, like Lu for an upper case letter or Nd for a decimal number,
  • the Unicode script names, like Arabic or Han,
  • and the derived properties Any and L&.

the full lists live in the pcrepattern and pcresyntax documentation β€” go look, it's a whole vibe.

only the short names for properties are supported. for example, \p{L} matches a letter. its Perl synonym, \p{Letter}, is not supported. πŸ™ˆ and also β€” in Perl, lots of properties can optionally be prefixed by "Is" for compatibility with Perl 5.6. PCRE does not support this. sorry babe πŸ’€


πŸ” Validity of UTF-8 strings πŸ”

when you set the PCRE_UTF8 flag, the byte strings passed as patterns and subjects are (by default) checked for validity on entry to the relevant functions. the entire string gets checked before any other processing takes place. thorough queen πŸ’–

from release 7.3 of PCRE, the check follows the rules of RFC 3629, which are themselves derived from the Unicode specification. earlier releases of PCRE followed the rules of RFC 2279, which allows the full range of 31-bit values (0 to 0x7FFFFFFF). the current check allows only values in the range U+0 to U+10FFFF, excluding the surrogate area. (from release 8.33 the so-called "non-character" code points are no longer excluded, because Unicode corrigendum #9 makes it clear that they should not be.) πŸ“

characters in the "Surrogate Area" of Unicode are reserved for use by UTF-16, where they're used in pairs to encode codepoints with values greater than 0xFFFF. the code points that are encoded by UTF-16 pairs are available independently in the UTF-8 and UTF-32 encodings. (in other words, the whole surrogate thing is a fudge for UTF-16 which unfortunately messes up UTF-8 and UTF-32. not me being salty about UTF-16 😭)

if an invalid UTF-8 string is passed to PCRE, an error return is given. at compile time, the only additional information is the offset to the first byte of the failing character. the run-time functions pcre_exec() and pcre_dfa_exec() also pass back this information, as well as a more detailed reason code if the caller has provided memory in which to do this. receipts provided 🧾

in some situations you may already know your strings are valid, and therefore want to skip these checks to improve performance β€” for example, a long subject string that's being scanned repeatedly. if you set the PCRE_NO_UTF8_CHECK flag at compile time or at run time, PCRE assumes that the pattern or subject it is given (respectively) contains only valid UTF-8 codes. in this case, it does not diagnose an invalid UTF-8 string. ✨

⚠️ note that passing PCRE_NO_UTF8_CHECK to pcre_compile() just disables the check for the pattern; it does not also apply to subject strings. if you want to disable the check for a subject string you must pass this option to pcre_exec() or pcre_dfa_exec().
πŸ’€ and if you pass an invalid UTF-8 string when PCRE_NO_UTF8_CHECK is set, the result is undefined and your program may crash. so like. be careful bestie. that's not a vibe.

πŸ” Validity of UTF-16 strings πŸ”

when you set the PCRE_UTF16 flag, the strings of 16-bit data units passed as patterns and subjects are (by default) checked for validity on entry to the relevant functions. values other than those in the surrogate range U+D800 to U+DFFF are independent code points. values in the surrogate range must be used in pairs in the correct manner. no freestyling 🫢

if an invalid UTF-16 string is passed to PCRE, an error return is given. at compile time, the only additional information is the offset to the first data unit of the failing character. the run-time functions pcre16_exec() and pcre16_dfa_exec() also pass back this information, as well as a more detailed reason code if the caller has provided memory in which to do this.

in some situations you may already know your strings are valid, and therefore want to skip these checks in order to improve performance. if you set the PCRE_NO_UTF16_CHECK flag at compile time or at run time, PCRE assumes that the pattern or subject it is given (respectively) contains only valid UTF-16 sequences. in this case, it does not diagnose an invalid UTF-16 string.

⚠️ however, if an invalid string is passed, the result is undefined.

πŸ” Validity of UTF-32 strings πŸ”

when you set the PCRE_UTF32 flag, the strings of 32-bit data units passed as patterns and subjects are (by default) checked for validity on entry to the relevant functions. this check allows only values in the range U+0 to U+10FFFF, excluding the surrogate area U+D800 to U+DFFF. 🌈

if an invalid UTF-32 string is passed to PCRE, an error return is given. at compile time, the only additional information is the offset to the first data unit of the failing character. the run-time functions pcre32_exec() and pcre32_dfa_exec() also pass back this information, as well as a more detailed reason code if the caller has provided memory in which to do this.

in some situations you may already know your strings are valid, and therefore want to skip these checks in order to improve performance. if you set the PCRE_NO_UTF32_CHECK flag at compile time or at run time, PCRE assumes that the pattern or subject it is given (respectively) contains only valid UTF-32 sequences. in this case, it does not diagnose an invalid UTF-32 string.

⚠️ however, if an invalid string is passed, the result is undefined.

🧚 General comments about UTF modes 🧚

buckle up, numbered list time, and every single one matters:

  1. codepoints less than 256 can be specified in patterns by either braced or unbraced hexadecimal escape sequences (for example, \x{b3} or \xb3). larger values have to use braced sequences.
  2. octal numbers up to \777 are recognized, and in UTF-8 mode they match two-byte characters for values greater than \177.
  3. repeat quantifiers apply to complete UTF characters, not to individual data units, for example: \x{100}{3}.
  4. the dot metacharacter matches one UTF character instead of a single data unit.
  5. the escape sequence \C can be used to match a single byte in UTF-8 mode, or a single 16-bit data unit in UTF-16 mode, or a single 32-bit data unit in UTF-32 mode β€” but its use can lead to some strange effects, because it breaks up multi-unit characters (see the description of \C in the pcrepattern documentation). the use of \C is not supported in the alternative matching function pcre[16|32]_dfa_exec(), nor is it supported in UTF mode by the JIT optimization of pcre[16|32]_exec(). if JIT optimization is requested for a UTF pattern that contains \C, it will not succeed, and so the matching will be carried out by the normal interpretive function. 🚩
  6. the character escapes \b, \B, \d, \D, \s, \S, \w, and \W correctly test characters of any code value, but by default, the characters that PCRE recognizes as digits, spaces, or word characters remain the same set as in non-UTF mode, all with values less than 256. this remains true even when PCRE is built to include Unicode property support, because to do otherwise would slow down PCRE in many common cases. note in particular that this applies to \b and \B, because they are defined in terms of \w and \W. if you really want to test for a wider sense of, say, "digit", you can use explicit Unicode property tests such as \p{Nd}. alternatively, if you set the PCRE_UCP option, the way that the character escapes work is changed so that Unicode properties are used to determine which characters match. there are more details in the section on generic character types in the pcrepattern documentation. πŸ‘‘
  7. similarly, characters that match the POSIX named character classes are all low-valued characters, unless the PCRE_UCP option is set.
  8. however, the horizontal and vertical white space matching escapes (\h, \H, \v, and \V) do match all the appropriate Unicode characters, whether or not PCRE_UCP is set. slay ✨
  9. case-insensitive matching applies only to characters whose values are less than 128, unless PCRE is built with Unicode property support. a few Unicode characters such as Greek sigma have more than two codepoints that are case-equivalent. up to and including PCRE release 8.31, only one-to-one case mappings were supported, but later releases (with Unicode property support) do treat as case-equivalent all versions of characters such as Greek sigma. πŸ›οΈ

πŸŽ€ AUTHOR πŸŽ€

Philip Hazel University Computing Service Cambridge CB2 3QH, England.


πŸŽ€ REVISION πŸŽ€

last updated: 27 February 2013 Copyright (c) 1997-2013 University of Cambridge.


and that's the whole page bestie, you ate that πŸ’…βœ¨ this was PCREUNICODE(3) from PCRE 8.33, dated 27 February 2013 β€” copyright held down by the University of Cambridge, and the source/version line says PCRE 8.33 with the date 27 February 2013. go forth and match responsibly πŸ¦‹πŸ’–