XML technologies / Regular expressions / Features and dialects / Strings, character encodings, and modes / Unicode
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Unicode is often misunderstood. First, it is a character set or conceptual encoding: a mapping between numbers and characters. The Korean character 삵, for example, is numbered 49,333. This number is a code point , usually written in hexadecimal with a U+ prefix. 49,333 is C0B5 in hex, so 삵 is U+C0B5. Each character has attributes, such as “3 is a digit” or “ê is lowercase, with uppercase counterpart Ê”.
This says nothing about representing numbers in computers. Options include UCS-2 encoding, with two bytes per character, UCS-4 with four bytes, UTF-16 with usually two bytes, sometimes four, and UTF-8 with one to six bytes depending on the character. Internal encoding normally need not concern users, except for converting external data from ASCII, Latin-1, UTF-8, etc. into the program's encoding. Unicode-aware programs generally supply conversion routines.
Unicode-aware regexes usually support \uZahl to match the specified character (see Hexadecimal and Unicode escapes: \xZahl, \x{Zahl}, \uZahl, \UZahl, ...). The Zahl usually requires four hexadecimal digits, so \uC0B5 matches 삵. Saying “\uC0B5 matches Unicode character U+C0B5” says nothing about the bytes compared; those depend on internal encoding. UTF-8 uses three bytes for 삵. Users rarely need to care, except, for example, with the preg functions of PHP with the u modifier.
Some related details may nevertheless interest you…
Characters and character combinations
Human “characters” need not match Unicode's or a program's definition. Most people consider à one character, but Unicode can build it from two: U+0061 (a) followed by U+0300 (`). Unicode has combining characters following ordinary characters, called base characters , and combining into a new character. This complicates the regex engine: should dot match one code point or the entire combination U+0061 plus U+0300?
Programs usually treat characters and code points as synonyms, regardless of combinations. Thus, ˹^..$˼ matches à (U+0061 plus U+0300), unlike ˹^.$˼.
In Perl and PCRE, hence PHP’s preg functions, the sequence \X is available, approximating any character with its following combining characters. See Unicode combining-character sequence: \X.
Remember this when editing code: if Å is entered as A plus a ring, it is unlikely to match single-code-point Å. The engine sees two characters. In a class, ˹[...Å...]˼ adds two characters, as if explicitly writing ˹[...A˚...]˼.
Likewise, a quantifier following two-code-point Å applies only to the second point, as if writing ˹A˚+˼.
Multiple code points for the same character
Unicode should theoretically map characters one-to-one, but some have multiple representations. Above, à was represented as U+0061 followed by U+0300. However, à can also be represented by the single point U+00E0. This duplication eases conversion from Latin-1. Converting a Latin-1 document to Unicode will probably map à, hexadecimal e0 in Latin-1, to U+00E0, though U+0061 plus U+0300 also works. Usually both must be checked. Sun's java.util.regex package offers CANON_EQ to treat canonically equivalent forms alike (see table java.util.regex modes).
Similarly, different characters can look alike, confusing matching. Our capital I (U+0049) resembles Greek iota (Ι, U+0399). With a diaeresis, Ï and Ï already have four possible Unicode representations (U+00CF; U+03AA; U+0049 U+0308; U+0399 U+0308). Matching Ï may require four checks. Such ambiguities are common.
Single code points can also resemble multiple characters. Unicode's SQUARE HZ (U+3390), Hz, representing hertz, looks like the two characters Hz (U+0048 U+007A).
Hz is rare, but defined characters get used, increasing software complexity. Similar cases include space (U+0020) and nonbreaking space (no-break space, U+00A0), which prevents line breaks. Unicode has roughly a dozen spaces.
Unicode 3.1+ and code points beyond U+FFFF
Unicode 3.1 added points beyond U+FFFF in 2001. Earlier versions allowed but had not assigned these positions. For example, the
has code point U+1D121. Older programs restricted to U+FFFF cannot handle it. Most languages allow only four hex digits in \uZahl.
Programs supporting these new points usually use \x{Zahl}, where Zahl has any number of digits, instead of \uZahl with exactly four. Thus, the C clef is \x{1D121}.
Unicode line endings
Unicode defines several possible line-ending characters, plus a two-character sequence shown in the table.
Table: Unicode line endings.
Character | Code point | Description |
|---|---|---|
LF | U+000A | ASCII Line Feed |
VT | U+000B | ASCII Vertical Tab |
FF | U+000C | ASCII Form Feed |
CR | U+000D | ASCII Carriage Return |
CR/LF | U+000D U+000A | ASCII Carriage Return / Line Feed (combination) |
NEL | U+0085 | Unicode NEXT LINE |
LS | U+2028 | Unicode LINE SEPARATOR |
PS | U+2029 | Unicode PARAGRAPH SEPARATOR |
Full support affects reading text files and, for scripting languages, reading source files. Regex behavior changes for dot (see Dot-matches-all mode) and the positions matched by ˹^˼, ˹$˼, and ˹\Z˼ (see Enhanced-line-anchor or multiline mode).
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln