Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this chapter, we will learn to use repetition metacharacters to gain more matching power in our regular expressions. We're going to start by looking at the three main metacharacters: the star--or asterisk--the plus, and the question mark. Each one of these metacharacters has an effect on the item that immediately precedes it. That item could be a literal character, it could be a shorthand character set, it could be a more complicated expression that we haven't even learned yet, but it takes that preceding item and determines how many times that item can be repeated. In the case of the asterisk, that item can be there 0 or more times; in the case of the plus, it would be there one or more times; and with the question mark, the item would just be there 0 time or one time.
Now, it may be weird to think about repetition in terms of something being there 0 times, but it does make sense. What we're talking about is really quantifying the fact that this item is repeatable or is not repeatable. Now, take a second and notice that out of these three metacharacters, only the plus sign says the item actually must exist. Both the asterisk and the question mark allow for the possibility that the item doesn't exist at all. Let's take a look at some concrete examples. Let's say I have a regular expression with the literal characters A-P-P-L-E-S followed by the asterisk. First, let's note that the asterisk only applies to the S, which is the immediately preceding item.
We'll learn ways later, if we wanted, to repeat the word apples. We could do that too. But, for now, it just matches the literal S. So it would match the strings apple, apples, and applesssssss with a whole bunch of Ss after it. So notice that it matches apple without the S because the S is optional. It's 0 or more times, so 0 is fine. One time is fine, two times, three times, four times, it doesn't have a limit on it. It says the item may or may not be there and if it's there, it could be repeated. Now, with a simple word like apples, maybe you're thinking well, that's kind of silly. Why would I ever want to look for apples with a whole bunch of Ss after it? But imagine that you had a file where some of the words had tabs separating them and you wanted to search for things that might have a tab between words.
It might not have a tab between words; it might have five tabs between words; you don't care about that in your matching; you still want it to match those words regardless of how many tabs are in between them. That's when you would use this. Let's compare that to the plus metacharacter. The plus matches apples and applesssssss with all the Ss, but not the single apple because the plus says that the S must exist. It must be there, but if it's there, it can be repeated. And then last of all, we have the question mark, which says that it can be there or not there--it's the optional metacharacter--but never repeated.
It's not repeatable; there can't be two of them; there can't be three of them, only 0 or 1. Now, as I said, we don't just have to put a literal character there; we could use a character class or character set. I'm going to use the shorthand character set \d and show you how you could match numbers with three digits or more using the star. Don't be tripped up here. Notice that we're talking about three digits or more, even though you see four of those \ds. Remember, that last one is optional. It's exactly the same as if we wrote it this way,\d\d\d, three of those \ds followed by the plus sign-- that's three digits or more.
Now typically, I would write it the second way, because I think that's clearer and you're less likely to make mistakes. But, I want you to see that they are the same thing. Also, don't be fooled into thinking that this means that the actual digit at the end has to be repeated. It's not 1233333333 and it has to be the 3 or it won't match; it could also be 123456789. It's the expression, or in this case the digit character set, that gets repeated. Then another classic use of the question mark metacharacter is to say that a letter is optional.
So for example, we match color either with or without the U by just saying hey! This u, question mark after it, it's optional. It may or may not be there; it would match it in both cases. Now, as far as support for these, they're supported in most regular expression engines. The one exception is that in really old UNIX programs, original programs like grep do not support plus and question mark, only the asterisk. So just keep that in mind. If you're working with really old UNIX programs, you may not have built-in support for this plus or this question mark, or you may need to use an extra option like grep has where you use a -e option to be able to use extended regular expressions.
Let's try some out. All right! Let's start with our very simple example here. Let's try apple, apples, and applesssssss. One, two, three, four, fix, six, seven. There we go! Now, for our regular expression, we're going to just put in apple. You see of course that just matches apple by itself. If I put in the S at the end, it just matches the apples. If we put the asterisk at the end, it matches all three of them. If I put in the plus, it matches just those last two because now the S is required or it won't match. And then the last possibility is that we put the question mark in. In this case, it matches the first two or it matches apples here, but not the whole thing.
So it does match a partial match, but not a full match. Let's try our digit example. Let's say that we have digit\digit\ digit, and let's use the asterisked version first, just so we can see that. 123456789 and then let's do 1234 and 123 and 12, just so we can see. So notice, it does match three digits or more, all right? It does match this one. Even though it's got four Ds up here, it does match these three digits here. Notice that none of these digits are repeated; it's the digit class that's being repeated each time.
If I put a plus here, now it's four digits or more. I take out one, now it matches three digits or more the exact same way. Let's try another example. This is one that wasn't in the slide, so take everything out of this. And let's put-in a-z+\da-z* and let's try and match abc9xyz, okay? So that matches; it makes sense. It's got characters that are repeated at the front, a digit, and then characters are repeated at the end.
What if we take away some of those characters? Let's say that we take away these first two. It still matches right, because the plus says that the first character is there one or more times. If I take it away though, now we no longer have a match. It's required to have at least one character before it. Let's try the other way. If we go back here, we take away two characters, no big deal, because it is there one or more times. If we take the Z completely away, we still have a match, because it is possible we told it, that it doesn't exist at all; it is still optional. Then last of all, let's just look at that color example, ?r, and then let's do colour or color. Both of them match.
Of course colouur with two U's doesn't match. All right! Let's try another one. Let's put a simple phrase in here, and we say, "We picked apples." And then up here for a regular expression, what we're going to do is look for any word character followed by a plus sign and then a literal S after it. Essentially, what we're saying is find words that end in S. It's a very simple way to just say find me all words that end in S. So we found, "We picked apples." It finds apples, and says, ah! That word ends in S. So you can see how repetition characters are very useful. And as we saw before, they do allow for unlimited numbers of items in there.
That still matches just as well. If we want to limit the number that it matches and put an upper limit on it and cap it, well then to do that, we need to use quantified repetition, and that's what we'll look at in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96920 Viewers
56 Video lessons · 110602 Viewers
71 Video lessons · 79434 Viewers
131 Video lessons · 38113 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.