C.W.K.
Stream
Lesson 06 of 12 · published

Unicode Categories — \p{...}

~10 min · unicode, properties

Level 0Pattern-Curious
0 XP0/90 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

Match by Unicode property, not by listing characters

\p{...} matches any character with the given Unicode property. The complement is \P{...}. This unlocks portable Unicode-aware patterns without listing every character.

Common properties

  • \p{L} — any letter (any script)
  • \p{Ll} — lowercase letter
  • \p{Lu} — uppercase letter
  • \p{N} — any number-like (digits, fractions, Roman numerals, etc.)
  • \p{Nd} — decimal digit (any script's digits)
  • \p{P} — punctuation
  • \p{S} — symbol (math, currency, modifier)
  • \p{Z} — separator (space, line)
  • \p{Mn} — non-spacing mark (combining diacritics)

Script properties

\p{Script=Hangul} matches Korean Hangul syllables. \p{Script=Han} matches CJK ideographs. \p{Script=Latin} for Latin script. Useful for filtering by writing system.

Engine support

  • Python regex third-party: Full support.
  • Python re: No \p{...}, but \d, \w, \s are Unicode-aware by default.
  • JavaScript: Yes, with /u flag.
  • PCRE, Java, .NET, Ruby: Yes.
  • Go RE2: Yes (uses RE2 syntax).

When to use

Anytime you're processing multilingual text. [\p{L}\p{N}_] is a portable Unicode-aware identifier pattern, working for Korean variable names, Cyrillic, Arabic, anything. [a-zA-Z0-9_] only works for ASCII.

Code

Unicode property patterns·python
# Python — needs the third-party 'regex' module for \p{...}
import regex

# All letters in any script
regex.findall(r'\p{L}+', 'hello 안녕 123')
# ['hello', '안녕']

# Korean Hangul only
regex.findall(r'\p{Script=Hangul}+', 'hello 안녕 こんにちは')
# ['안녕']

# CJK ideographs
regex.findall(r'\p{Script=Han}+', '中文 한자 漢字')
# ['中文', '漢字']  — '한자' is Hangul phonetic, not Han

# Identifier pattern (Unicode-aware)
regex.findall(r'[\p{L}\p{N}_]+', 'var_name = 변수명 + 한글_var')
# ['var_name', '변수명', '한글_var']

# Strip combining marks (e.g., remove diacritics from text)
import unicodedata
clean = regex.sub(r'\p{Mn}', '',
                  unicodedata.normalize('NFD', 'café résumé'))
# 'cafe resume'

# JavaScript equivalent (with /u)
// '안녕 hello'.match(/\p{Script=Hangul}+/gu)
// ['안녕']

External links

Exercise

Take any multilingual text (mix of English, Korean, emoji). Write three patterns: extract all letters across scripts (\p{L}+), extract all numbers including non-Latin digits (\p{N}+), extract all Hangul (\p{Script=Hangul}+). Compare to what \w+ would catch in ASCII vs Unicode-aware mode.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.