Unicode character properties

Since 5.1.0, three additional escape sequences to match generic character types are available when UTF-8 mode is selected. They are:

\p{xx}
a character with the xx property
\P{xx}
a character without the xx property
\X
an extended Unicode sequence

The property names represented by xx above are limited to the Unicode general category properties. Each character has exactly one such property, specified by a two-letter abbreviation. For compatibility with Perl, negation can be specified by including a circumflex between the opening brace and the property name. For example, \p{^Lu} is the same as \P{Lu}.

If only one letter is specified with \p or \P, it includes all the properties that start with that letter. In this case, in the absence of negation, the curly brackets in the escape sequence are optional; these two examples have the same effect:

\p{L}
\pL
Supported property codes
Property Matches Notes
C Other  
Cc Control  
Cf Format  
Cn Unassigned  
Co Private use  
Cs Surrogate  
L Letter Includes the following properties: Ll, Lm, Lo, Lt and Lu.
Ll Lower case letter  
Lm Modifier letter  
Lo Other letter  
Lt Title case letter  
Lu Upper case letter  
M Mark  
Mc Spacing mark  
Me Enclosing mark  
Mn Non-spacing mark  
N Number  
Nd Decimal number  
Nl Letter number  
No Other number  
P Punctuation  
Pc Connector punctuation  
Pd Dash punctuation  
Pe Close punctuation  
Pf Final punctuation  
Pi Initial punctuation  
Po Other punctuation  
Ps Open punctuation  
S Symbol  
Sc Currency symbol  
Sk Modifier symbol  
Sm Mathematical symbol  
So Other symbol  
Z Separator  
Zl Line separator  
Zp Paragraph separator  
Zs Space separator  

Extended properties such as "Greek" or "InMusicalSymbols" are not supported by PCRE.

Specifying case-insensitive (caseless) matching does not affect these escape sequences. For example, \p{Lu} always matches only upper case letters.

The \X escape matches any number of Unicode characters that form an extended Unicode sequence. \X is equivalent to (?>\PM\pM*).

That is, it matches a character without the "mark" property, followed by zero or more characters with the "mark" property, and treats the sequence as an atomic group (see below). Characters with the "mark" property are typically accents that affect the preceding character.

Matching characters by Unicode property is not fast, because PCRE has to search a structure that contains data for over fifteen thousand characters. That is why the traditional escape sequences such as \d and \w do not use Unicode properties in PCRE.

add a note add a note

User Contributed Notes 8 notes

up
16
huhwatnouDONTspamPLEASE at hotmail dot com
8 years ago
To select UTF-8 mode for the additional escape sequences (\p{xx}, \P{xx}, and \X) , use the "u" modifier (see http://php.net/manual/en/reference.pcre.pattern.modifiers.php).

I wondered why a German sharp S (ß) was marked as a control character by \p{Cc} and it took me a while to properly read the first sentence: "Since 5.1.0, three additional escape sequences to match generic character types are available when UTF-8 mode is selected. " :-$ and then to find out how to do so.
up
8
mercury at caucasus dot net
13 years ago
An excellent article explaining all these properties can be found here: http://www.regular-expressions.info/unicode.html
up
11
xuantoaiph at gmail dot com
10 years ago
My country, Vietnam, have our own alphabet table:
http://en.wikipedia.org/wiki/Vietnamese_alphabet
I hope PHP will support better than in Vietnamese.
up
4
o_shes01 at uni-muenster dot de
13 years ago
For those who wonder: 'letter_titlecase' applies to digraphs/trigraphs, where capitalization involves only the first letter.
For example, there are three codepoints for the "LJ" digraph in Unicode:
  (*) uppercase "LJ": U+01C7
  (*) titlecase "Lj": U+01C8
  (*) lowercase "lj": U+01C9
up
3
suit at rebell dot at
14 years ago
these properties are usualy only available if PCRE is compiled with "--enable-unicode-properties"

if you want to match any word but want to provide a fallback, you can do something like that:

<?php
if(@preg_match_all('/\p{L}+/u', $str, $arr) {
 
// fallback goes here
  // for example just '/\w+/u' for a less acurate match
}
?>
up
1
Yzmir Ramirez
10 years ago
If you are working with older environments you will need to first check to see if the version of PCRE will work with unicode directives described above:

<?php

// Need to check PCRE version because some environments are
// running older versions of the PCRE library
// (run in *nix environment `pcretest -C`)

$allowInternational = false;
if (
defined('PCRE_VERSION')) {
    if (
intval(PCRE_VERSION) >= 7) { // constant available since PHP 5.2.4
       
$allowInternational = true;
    }
}
?>

Now you can do a fallback regex (e.g. use "/[a-z]/i"), when the PCRE library version is too old or not available.
up
-2
php at lnx-bsp dot net
6 years ago
Not made clear in the top of page explanation, but these escaped character classes can be included within square brackets to make a broader character class. For example:

<?php preg_match( '/[\p{N}\p{L}]+/', $data ) ?>

Will match any combination of letters and numbers.
up
-4
o_shes01 at uni-muenster dot de
13 years ago
For those who wonder: 'letter_titlecase' applies to digraphs/trigraphs, where capitalization involves only the first letter.
For example, there are three codepoints for the "LJ" digraph in Unicode:
  (*) uppercase "LJ": U+01C7
  (*) titlecase "Lj": U+01C8
  (*) lowercase "lj": U+01C9
To Top