Regex Character Classes
Character Class Constructs
A character class allows you to specify a set of characters, enclosed in square brackets, that can produce a single character match. A character class matches to a single character only. For example,
gr[ae]y
will match to gray
or grey
, but not to graay
or graey
. The order of the characters inside the brackets doesn't matter.You can use a hyphen inside a character class to specify a range of characters. Example:
[a-z]
matches a single lower-case letter between a
and z
. You can also use more than 1 range, or a combination of ranges and single characters. Example: [0-9X]
matches a numeric digit or the letter X
. The order of the characters and the ranges doesn't matter.A caret following an opening bracket specifies characters to exclude from a match. For example,
[^abc]
matches any character except a
, b
, or c
.Construct | Type | Description |
|---|---|---|
[
abc ] | Simple | Matches a or b or c
|
[^abc]
| Negation | Matches any character except a or b or c
|
[a-zA-Z]
| Range | Matches a through z , or A through Z (inclusive) |
[a-d[m-p]]
| Union | Matches a through d , or m through p
|
[a-z&&[def]]
| Intersection | Matches d , e , or f
|
[a-z&&[^xq]]
| Subtraction | Matches a through z , except for x and q
|
Predefined Character Classes
Predefined character classes are convenient shorthands for commonly used regular expressions.
Construct | Description | Example |
|---|---|---|
.
| Matches any single character (except newline) | .at matches "cat", "hat", and also "bat" in the phrase "batch files" |
\d
| Matches any digit character (equivalent to [0-9] ) | \d matches "3" in "C3PO" and "2" in "file_2.txt" |
\D
| Matches any non-digit character (equivalent to [^0-9] ) | \D matches "S" in "900S" and "Q" in "Q45" |
\s
| Matches any single white-space character (equivalent to [ \t\n\x0B\f\r] ) | \sbook matches "book" in "blue book" but nothing in "notebook" |
\S
| Matches any single non-white-space character | \Sbook matches "book" in "notebook" but nothing in "blue book" |
\w
| Matches any alphanumeric character, including underscore (equivalent to [A-Za-z0-9_] ) | r\w* matches "rm" and "root" |
\W
| Matches any non-alphanumeric character (equivalent to [^A-Za-z0-9_] ) | \W matches "&" in "stmd &", "%" in "100%", and "$" in "$HOME" |
POSIX Character Classes (US-ASCII)
POSIX has a set of character classes that denote certain common ranges. They're similar to bracket and predefined character classes, except they take into account the locale (the local language/coding system).
Construct | Description |
|---|---|
\p{Lower}
| A lower-case alphabetic character, [a-z]
|
\p{Upper}
| An upper-case alphabetic character, [A-Z]
|
\p{ASCII}
| An ASCII character, [\x00-\x7F]
|
\p{Alpha}
| An alphabetic character, [a-zA-z]
|
\p{Digit}
| A numeric digit, [0-9]
|
\p{Alnum}
| An alphanumeric character, [a-zA-z0-9]
|
\p{Punct}
| A punctuation character, one of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
|
\p{Graph}
| A visible character, [\p{Alnum}\p{Punct}]
|
\p{Print}
| A printable character, [\p{Graph}\x20]
|
\p{Blank}
| A space or tab, [ t]
|
\p{Cntrl}
| A control character, [\x00-\x1F\x7F]
|
\p{XDigit}
| A hexadecimal digit, [0-9a-fA-F]
|
\p{Space}
| A whitespace character, [ \t\n\x0B\f\r]
|