Skip to content

Unicode and flags

What is a character, exactly?

The u flag, and the day your pattern met an emoji.

Without u, a pattern reads the input as UTF-16 code units. Most characters are one unit and nothing seems amiss. Anything above U+FFFF — every emoji, every historic script, half of the mathematical alphabet — is two units, and . will happily match one of them.

Half of a surrogate pair is not a character. It is a fragment that renders as nothing and breaks whatever it is concatenated with.

u also unlocks \u{...}, which names a code point by number rather than by surrogate pair, and makes several sloppy escapes into errors instead. That last part is the real value: a pattern that compiles under u is a pattern with no typos of that kind in it.

Matcher

Every claim on the left has a pattern under it. Press one and it runs here, against the same engine that grades a task.