Shorthand characters

Before moving on to Repetition characters, let’s first learn about the shorthand characters that represent the ranges-0-9, a-z, and A-Z separately and together. You’re pretty familiar with the range characters and how to use them with the character set. You wrote a RegEx that consists of two character sets [^A-Za-z0-9_] in the previous challenge. Challenge to understand the negative character set. Rather than expressing this entire bunch of characters, we can represent them more succinctly by using \w Backslash with lowercase “w”. It will represent a word character set. Similarly, a negative word character set will be represented by a backslash with an uppercase “W”.

Here’s the list of all the shorthand characters.


Shorthand Explanation Equivalent
\W Not word character [^a-zA-Z0-9_]
\w Word character [a-zA-Z0-9_]
\S Not whitespace [^ \t\r\n]
\s Whitespace [ \t\r\n]
\D Not digit [^0-9]
\d Digit [0-9]

Now you understand why we should not escape the literal characters. This technique makes the RegEx more readable and reduces its complexity.

Get hands-on with 1400+ tech skills courses.