Use Shorthand Regular Expression Character Classes

Joe Maddalone
InstructorJoe Maddalone
Share this video with your friends

Social Share Links

Send Tweet

In this lesson we'll learn shorthands for common character classes as well as their negated forms.

[00:00] If we wanted to match all alphanumeric characters we can use this character class of all lower case letters, all upper case letters, and all digits from 0to 9. Save that, and we can see that we are in fact matching all of those characters. However there's a shorthand for this, and in this case it is \w, which is going to give us the exact same results, matching all of those characters, but not spaces, and not symbols or Unicode characters.

[00:30] Now if we only want digits we can do \d, and now we're matching only the digits, and if we only want to match white space, we can do \s and now we're only matching the white space. Now since these are character classes they can be negated, so I'm creating a new character class, I'm starting it off with the caret symbol which is going to negate whatever follows it, and I can just drop my character class of alphanumeric right here after that, so now we are matching only white space and symbols, we're not matching any letters or numbers.

[01:04] We can do the same thing with digits, so now we're matching everything that's not a digit. We can do the same thing with white space, and we're matching everything that's not white space. Now there's actually shorthands for the negated versions of each of these shorthands, and the first one would be \W, so that's going to negate the alphanumeric matching, so now we're matching white space and symbols.

[01:27] If we want to negate matching digits we can do \D and now we're matching everything except for digits, and if we want to do the negation of white space, we can do \S and now we're matching everything except for white space.