Regex | Matches |
---|---|
[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 |
Some groups/ranges of characters are so often used, they have special abbreviations:
Regex | Matches |
---|---|
\w | Alphanumeric characters plus the underscore (also referred to as "word characters") |
\W | Non-word characters (same as [^\w] ) |
\d | Digits (wider than [0-9] since include Persian digits, Indian ones etc.) |
\D | Non-digits (shorter than [^0-9] since reject Persian digits, Indian ones etc.) |
\s | Whitespace characters (spaces, tabs, etc...) Note: may vary depending on your engine/context |
\S | Non-whitespace characters |
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).
Regex | Matches |
---|---|
[^AB] | Any character other than A and B |
[^\d] | Any character, except digits |