Regular Expressions Character classes

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

Simple classes

RegexMatches
[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:

RegexMatches
\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).

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


Got any Regular Expressions Question?