Regular Expressions Character classes


Simple classes

[abc]Any of the following characters: a, b, or c
[a-z]Any character from a to z, inclusive (this is called a range)
[0-9]Any digit from 0 to 9, inclusive

Common classes

Some groups/ranges of characters are so often used, they have special abbreviations:

\wAlphanumeric characters plus the underscore (also referred to as "word characters")
\WNon-word characters (same as [^\w])
\dDigits (wider than [0-9] since include Persian digits, Indian ones etc.)
\DNon-digits (shorter than [^0-9] since reject Persian digits, Indian ones etc.)
\sWhitespace characters (spaces, tabs, etc...) Note: may vary depending on your engine/context
\SNon-whitespace characters

Negating classes

A caret (^) after the opening square bracket works as a negation of the characters that follow it. This will match all characters that are not in the character class.

Negated character classes also match line break characters, therefore if these are not to be matched, the specific line break characters must be added to the class (\r and/or \n).

[^AB]Any character other than A and B
[^\d]Any character, except digits