Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We learned how to define character sets, and then we saw how range can save us a lot of typing inside those character sets. Now we're going to learn to save even more typing by learning about shorthand character sets. Shorthand character sets all begin with a backslash, followed by a letter: d for digit, w for word character, and s for whitespace. Don't let that through you off, the fact that whitespace is actually s for space. w is for word character. Then we have the three capital versions as well--capital D, capital W, and capital S--which are the opposite--not a digit, not a word character, and not whitespace.
You can see that in the table, I've written out the shorthand on the far left and the equivalent on the far right. Notice in the equivalent for whitespace that the whitespace is equal to a space, a tab, or line return; it's any of those things. Notice also that in the word character that it's upper- and lowercase letters as well as numbers and underscore. It's because regular expressions have their foundation in UNIX, and in UNIX it's very common to have underscores and numbers in a file name, so therefore they're allowed to be word characters here, even though we normally don't think about that.
So you can see that the shorthands are much shorter than writing the equivalent version, and that can save you a lot of time, and it can also help reduce mistakes. But it can also lead to mistakes if you don't think about the fact of what these are equivalent to, especially with that word character and the whitespace. Another important point about the W is that underscore is a word character, but hyphen is not a word character; it's considered punctuation. So be careful about that and don't let that throw you off either. Underscore is included, hyphen is not, and also, digits are included as well.
Let's take a look at some examples. So if we have the shorthand for digit four times, then that's a four-digit number. It matches 1984, but it does not match a four-letter word, like text. Now if we have the word character shortcut three times, that would match ABC, which makes sense that seems like a word character, but it also matches 123, which you would think, oh wait, that was digits--it should be a different thing. No, it's still included here, as well as 1_A, which looks nothing like a word, but still, it's made up of what are considered word characters.
Be careful with that. And then I've showed you how to use the space character. I've got a word character, followed by a space character, followed by two word characters, which matches I am, but not Am I. Now of course, if you really wanted just a real space, there is no need to use the special character; you can just put a literal space in there and that'll match. This is only if it could also be a tab or line return in there. If you want to allow those possibilities then you use this. You can also put these character sets inside a character set.
So for example, we could have a character set that is made up of any word character and the hyphen. That can be very useful for looking to include hyphenated words in our matches. We can also say well, we're looking for any one character that is a digit character or a whitespace character, so we can combine those side by side. We can also use negation with them, so we could have anything that's not a digit. Of course that's the exact same thing as if we'd use the not-a-digit shorthand or written it out the long way. A word of caution here though about using negatives, especially when we already have these negative short hands: if we use a character set where we negate a digit or whitespace, that's not the same thing as a character set that is not a digit or not a whitespace.
Let me break it down and show you the difference. The first one says the whole thing is negated; it's not a digit or whitespace character. The second one says either. It's either not a digit or it's not a space character. The negative is applied differently in each case. We'll take a closer look at that in the examples. Now as far support for the shorthand character sets, in general, they're going to be in all regular expression engines. They started out with Perl and they really spread very quickly, because they're so useful, to all modern regex engines, so all languages are going to support them.
A lot of the older UNIX tools are not, so you'll have to write these out the long way; you can't use the shorthand. So for that reason, if you're writing a regular expression that really needs to be portable and needs to be able to be used in the UNIX environment or inside a UNIX tool then you will want to write it out the long way. But if you're just using it inside your programming language--let's say you're programming something in Perl--then go ahead and feel free to use these shorthands. Alright, let's try this out in regexPal. So to start with, let's do our four digits--d, d, d, d--and that should match 1984, but it should not match text. Nothing special there.
Let's try up here, let's put our w, w, w. Now notice this matched both the digits-- and I'll go and put a forth one-- it matched both the digits and it matched the word. If we had something here that was 1_5W, it matches that too. Let's talk about hyphens for a second. Let's say we had blue-green paint, right, and we want to match that. If we just had a W, it matches everything but the hyphen and the space there.
If we wanted to include that hyphen then you make a character set and in that character set we need to put our hyphen and remember, we want to escape the hyphen whenever it's in a character set as well. So now it is says anything that's a word character or a hyphen. Let's try another character set, let's put down here 123456789 and then abc, so now up here, let's put in our character set a digit or a space. Anything that's a digit or a space is what we want to match. So you can see it did that, and it did not match abc.
If we said we want anything that is not a digit or space, you can see we get the opposite of that right. It negated it, and it gave us what we would expect: a, b and c. Now to show you the difference, let's look at this one. What we're saying here is anything that is either not a digit or not a space. So it goes to the first one. It says all right, is this number one right here? Is that not a digit? No it's not. But is it not a space? Oh yes, it's not space, so it's a match. Then it goes to the next one, number 2, same thing. This is not a space; it qualifies.
When it gets to the space between them it say ah! This is not a digit; it qualifies. So you see how the negation there is applied differently between the two. I think most of the time you will just find yourself using the lowercase ones anyway, but if you do start to use those uppercase ones and you start to combine them together, just take that extra step to logically think through what it's going to match, and try it on a few samples to make sure you've got it right.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96725 Viewers
56 Video lessons · 110435 Viewers
71 Video lessons · 79267 Viewers
131 Video lessons · 38010 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.