Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Quantified repetition

From: Using Regular Expressions

Video: Quantified repetition

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.

Quantified repetition

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.

Show transcript

This video is part of

Image for Using Regular Expressions
Using Regular Expressions

59 video lessons · 11695 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 2m 18s
    1. Welcome
      56s
    2. Using the exercise files
      1m 22s
  2. 19m 55s
    1. What are regular expressions?
      3m 20s
    2. The history of regular expressions
      6m 40s
    3. Regular expression engines
      2m 44s
    4. Installing an engine
      4m 5s
    5. Notation conventions and modes
      3m 6s
  3. 21m 23s
    1. Literal characters
      6m 39s
    2. Metacharacters
      2m 1s
    3. The wildcard metacharacter
      4m 31s
    4. Escaping metacharacters
      4m 53s
    5. Other special characters
      3m 19s
  4. 31m 26s
    1. Defining a character set
      5m 49s
    2. Character ranges
      4m 49s
    3. Negative character sets
      4m 53s
    4. Metacharacters inside character sets
      5m 12s
    5. Shorthand character sets
      6m 30s
    6. POSIX bracket expressions
      4m 13s
  5. 36m 38s
    1. Repetition metacharacters
      7m 17s
    2. Quantified repetition
      6m 59s
    3. Greedy expressions
      6m 27s
    4. Lazy expressions
      6m 46s
    5. Using repetition efficiently
      9m 9s
  6. 20m 24s
    1. Grouping metacharacters
      4m 14s
    2. Alternation metacharacter
      4m 54s
    3. Writing logical and efficient alternations
      7m 33s
    4. Repeating and nesting alternations
      3m 43s
  7. 19m 19s
    1. Start and end anchors
      7m 21s
    2. Line breaks and Multiline mode
      4m 41s
    3. Word boundaries
      7m 17s
  8. 23m 33s
    1. Backreferences
      8m 57s
    2. Backreferences to optional expressions
      3m 51s
    3. Finding and replacing using backreferences
      7m 16s
    4. Non-capturing group expressions
      3m 29s
  9. 32m 31s
    1. Positive lookahead assertions
      6m 39s
    2. Double-testing with lookahead assertions
      7m 16s
    3. Negative lookahead assertions
      6m 10s
    4. Lookbehind assertions
      6m 26s
    5. The power of positions
      6m 0s
  10. 13m 13s
    1. About Unicode
      4m 19s
    2. Unicode in regular expressions
      4m 41s
    3. Unicode wildcards and properties
      4m 13s
  11. 1h 55m
    1. How to use this chapter
      5m 38s
    2. Matching names
      6m 33s
    3. Matching postal codes
      8m 54s
    4. Matching email addresses
      5m 0s
    5. Matching URLs
      8m 1s
    6. Matching decimal numbers and currency
      6m 45s
    7. Matching IP addresses
      7m 10s
    8. Matching dates
      7m 49s
    9. Matching times
      8m 59s
    10. Matching HTML tags
      8m 34s
    11. Matching passwords
      6m 49s
    12. Matching credit card numbers
      9m 36s
    13. Finding words near other words
      6m 38s
    14. Formatting with Search and Replace, pt. 1
      7m 22s
    15. Formatting with Search and Replace, pt. 2
      4m 15s
    16. Formatting with Search and Replace, pt. 3
      7m 10s
  12. 47s
    1. Goodbye
      47s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Using Regular Expressions.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.