A character class matches any one of a set of characters. Character classes include the language elements listed in the following table.
Pattern | Description | Sample | Matches |
[ chars group ] | Matches any single character in character_group. By default, the match is case-sensitive. | [ae] | "a" in "gray", "a", "e" in "lane" |
[^ chars group ] | Negation: Matches any single character that is not in character_group. By default, characters in character_group are case-sensitive. | [^aei] | "r", "g", "n" in "reign" |
[ first - last ] | Character range: Matches any single character in the range from first to last. | [A-Z] | "A", "B" in "AB123" |
. | Wildcard: Matches any single character except \n. To match a literal period character (. or \u002E), you must precede it with the escape character (\.). | a.e | "ave" in "nave", "ate" in "water" |
\p{ name } | Matches any single character in the Unicode general category or named block specified by name. | \p{Lu} | "C", "L" in "City Lights" |
\P{ name } | Matches any single character that is not in the Unicode general category or named block specified by name. | \P{Lu} | "i", "t", "y" in "City" |
\w | Matches any word character. | \w | "I", "D", "A", "1", "3" in "ID A1.3" |
\W | Matches any non-word character. | \W | " ", "." in "ID A1.3" |
\s | Matches any white-space character. | \w\s | "D " in "ID A1.3" |
\S | Matches any non-white-space character. | \s\S | " _" in "int __ctr" |
\d | Matches any decimal digit. | \d | "4" in "4 = IV" |
\D | Matches any character other than a decimal digit. | \D | " ", "=", " ", "I", "V" in "4 = IV" |