Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the last movie, we learned about the basic repetition metacharacters, but as you saw with both the asterisk and the plus, they allow for an amount of unlimited repetition. So they allowed us to have this ridiculous example where we had apples with seven Ss after it. Now, we saw how we could deal with if something just had zero or one repetitions, but what if we need some more precision than that? Well, to do that, we need to use quantified repetition, and that's what we're going to learn in this movie. To do that, we need a couple of more metacharacters, and these are the opening and closing curly braces.
We'll see how to use them in just a minute. The curly braces are still going to have an effect on the preceding item, exactly the same as the plus, the asterisk, and the question mark did. The syntax for using them is going to be to put an opening curly brace and then minimum, maximum, and the closing curly brace. Now, min and max are not the actual words min and max; they're going to be positive numbers that you're going to drop in there that are going to quantify it for you and tell you how many times this thing should be repeated. Min must always been included.
In other words, there must always be a first number there. But it can be 0. If it's possible for something not to exist, well then you can put a 0 in there. Max is optional, and the comma is actually optional as well. Let's take a look at the three possible syntaxes for the ways that you could use this. Let's start with just a basic digit metacharacter shorthand class. So we've got a digit that is repeated, 4,8. That's a minimum of 4 times, a maximum of 8 times. That matches numbers with 4 and 8 digits.
Now, notice that it's a comma; it's not a hyphen. 4-8, that's not going to work for you. It's got to be a comma in between those. Now, if we only put one thing in there, essentially that min value, well then in that case, minimum also becomes the maximum. And so in that case, it matches something that has exactly 4 digits, that occurs 4 times, not 3, not 5. The last way that you can use it looks a bit odd, and that's where we have a comma still, but this time we've left out the max, and in that case, maximum becomes infinite.
So we don't have a character to represent infinite that we can easily just type in there, so instead we just leave the comma in and that lets us differentiate between the one and the line above it. The one above it says match exactly 4 times; the second one says a minimum of 4 times, and infinite maximum. Let's look at a couple of examples. If we have the \d and we were to have 0 for a minimum, comma, infinite maximum, well then that's the same thing as our star character. It can occur 0 times or an infinite number of times.
If we had one comma and left maximum blank, that's the same thing as our plus character. Those are equivalent to doing the same thing. To show you another example, let's say that we wanted to match phone numbers, a very common use case. We could say, well we want three digits, followed by a dash, followed by three digits, followed by a dash, followed by four digits. So we could just as easily write out the \d all those times, but instead, we're saying, precisely this number of times, repeat it. You don't have to type it out; you can use this quantified repetition as a shorthand.
In the last example I have said, all right! Look for bonds which are A or AA bonds, not AAA. Either A has to occur one time or it has to occur two times and that's it. Let's pop over to regexpal and try a few more. To test with, I'm going to open up one of the exercise files here. I have just a Shakespeare sonnet that I've given you. You can use any text really. I just want to have a nice little bit of text that I could paste in here and test it against. So in my regular expression, let's use any word character, so let's just say with a plus sign followed by a space.
So it's not finding every word because there's some punctuation in there as well, but it is finding a lot of them. Now, instead of this unlimited repetition here from the plus sign, let's put in our quantified repetition, and let's just put in a 5 to start with. So now when it's done as it's found every series of five characters, followed by an S. That's five-letter words, but it's also words like "compare" and "lovely" as well, "darling." Those get caught as well because they still qualify. We could of course put an S at the beginning as well, and we'll actually learn a better way to do that a little later.
For now, let's just stick with this so we can illustrate the point. Now, let's say that we want to find every set of characters that is either 2,5. So now it finds every minimum of 2 characters that's followed by 5 characters. If I change that to 3 characters, notice that it changed and the word "do" for example just became disqualified. Now, since our minimum is 3, do didn't meet that criteria. If we instead take away, take away those and put in 5 again, let's put a comma after 5, now it finds everything that is 5 for infinite.
Let's go down to 3. Everything that's 3 or longer, those all get counted now. So those are the different syntaxes. Let's try our phone number example. Say we have 555-867-5309, famous old song. And let's put-in \d and then we're going to have 3 digits and then I'll just copy that, and then a dash. I forgot to copy the backslash. And then a dash, and then this time we need 4. So now we've repeated it each of those times.
Now obviously you can come up with a much more complicated one that would allow for things outside the US. This is really just for US phone numbers. Let's try our other one. Let's say we had A 1,2 Bonds, and then down here, A Bonds AA Bonds, and AAA Bonds. See, it matches just the last two of that, but not the beginning. If we wanted to say for looking inside of text, maybe we're looking for things that have space in front of them, so then it disqualifies it.
And one last example, let's say that we have a couple of files that we're looking at, and these are the three names of the files. And we want to match any file that has a name, so word characters, unlimited repetition--we don't care how long it is--underscore, followed by 2 to 4 digits. So we're thinking about the year here. That's what it would represent. Like 1997 could be represented as either 2 or 4 digits. Note that it also does allow 3 digits in there. 3 would still be valid, and then a dash and then after that, /d, and we'll put in we're looking for something with another two digits, presumably something like the month.
So now, it finds report_1997-04, budget_03-04, but it does not match memo with a long set of numbers, dash, and a long set of numbers. The quantified repetition is keeping that from happening, whereas if we just had our plus sign--there we go--you can see that now it matches all three. So whenever possible, it's a really good practice to try and use quantified repetition. It will help you to find exactly the things you're looking for and not accidentally pick up matches that you didn't mean to catch because you wrote a regular expression that was too permissive.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105052 Viewers
56 Video lessons · 116836 Viewers
71 Video lessons · 86061 Viewers
131 Video lessons · 41164 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.