Skip to content

Unicode and flags

Properties, and what case means

\p{L} is the class \w should have been, and i is stranger than it looks.

\p{L} matches a letter — any letter, in any script. It is what most people mean when they reach for \w, and it is available only under u.

There are properties for the general categories (\p{Lu} for uppercase letters, \p{Nd} for decimal digits, \p{P} for punctuation) and for scripts (\p{Script=Greek}). \P{...} is the complement of each.

Case-insensitive matching is not lowercase both sides and compare. The flavour defines a canonical form for each character, and two characters match when their canonical forms agree — which is why [a-z] under i accepts A through Z and not the six punctuation characters that sit between Z and a.

Under u the rule changes to Unicode simple case folding, and things start folding together that do not look related. ſ — a long s, from an eighteenth-century book — folds to s. Greek sigma has two lowercase forms and all three fold together.

One consequence catches people out badly: \w grows under iu, because the flavour extends it with every character whose canonical form is one of the sixty-three. That adds the long s and the Kelvin sign — and since \b is defined by \w, the word boundaries move too.

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.