Aller au contenu
uencoder

Unicode Propriété Escapes en Regex

Unicode Général Catégorie et Binary propriété escapes pour matching script et caractère classes.

General Categories

Unicode property escapes of the form backslash-p{Category} match characters belonging to a Unicode general category or binary property. These require the Unicode flag u on the regular expression. General categories include Letter, Number, Punctuation, and Symbol, with subcategories like Uppercase_Letter and Decimal_Number. Property escapes enable matching by semantic class rather than code point ranges, which is more robust across Unicode versions.

\p{L}
Any letter, any script
matches A, 中, ا
\p{N}
Any number, any script
matches 1, ٢, Ⅷ
\p{P}
Any punctuation
matches ., ,, 。
\p{Sc}
Currency symbol
matches $, €, ¥
\p{Script=Han}
Characters of the Han script
matches Chinese chars

Binary Properties

Binary properties are boolean attributes that a character either has or lacks. Common binary properties include White_Space, Alphabetic, Emoji, and Emoji_Presentation. The Emoji property is useful for matching emoji code points, though combining sequences require additional handling. Negated property escapes use capital P and match characters lacking the property. Binary properties are stable across Unicode versions more than general categories.

Script Segmentation

The Script property identifies the writing system of a character, such as Latin, Cyrillic, or Han. The Script_Extensions property handles characters used in multiple scripts, such as digits shared between scripts. Matching by script is useful for validating input in a specific language or segmenting mixed-script text. The Unicode flag is mandatory for property escapes, and omitting it causes a syntax error rather than a silent ASCII-only match.

Outils associés

参考链接