Viewers: in countries Watching now:
Learn how to find and manipulate text quickly and easily using regular expressions. Author Kevin Skoglund covers the basic syntax of regular expressions, shows how to create flexible matching patterns, and demonstrates how the regular expression engine parses text to find matches. The course also covers referring back to previous matches with backreferences and creating complex matching patterns with lookaround assertions, and explores the most common applications of regular expressions.
In this movie, I want us to just take a look at repeating and nesting our alternations. You can repeat your alternations. It's just simply a group, and that group can be repeated, just like we were doing grouped expressions before and then putting repetitions on them. Keep in mind though that with the nested alternations that the first matched alternation does not have any effect on the future matches. So if we have a expression where AA is the first choice, BB is the second choice, and CC is the third choice, if we repeat it six times, if we have a string where the first thing that matches is AA, that does not mean that AA is the thing that has to be repeated over and over.
It's the same way as when we repeated something of the digit class. We weren't saying that if we match the digit 9 and we were looking for four of those, then we had to have 9 four times. It's the digit class that gets repeated four times. So here's the whole alternation that gets repeated four times. And of course, we can nest alternations as well, but you just need to be careful with it. First you just want to make sure to check your nesting carefully to make sure that it does logically make sense. It can get a little bit tricky. If you have a really long regular expression and you've got lots of square brackets and parentheses and curly braces in there, backslashes in front of those shorthand character sets, it makes a lot harder to see where the alternations are and make sure that it does parse carefully.
One good tip is to put these into a text document and put line returns between them to start with. Make sure that each one of the parts works out the way you think it should, and then you can remove the line returns at the end so that it becomes one long line again. Overall with nesting it becomes a trade-off between precision, readability, and efficiency. Remember the things we talked about. With efficiency working with alternations, that effect becomes compounded when we start nesting them inside each other. Let's go to regexpal and see. So to start with, let's just have a simple regular expression here, and we're going to say the regular expression is either going to be two digits or it's going to be the characters A to Z or A to Z again--two uppercase letters--and that whole thing can repeat three times.
That's going to match 112233 just fine, and it's also going to match AABBCC just fine. Match AA66ZZ or 11AA44. It doesn't have to be the same thing each time as it repeats. It's the alternation, a whole alternation expression, that's repeated. So it's a lot to fluctuate between which one matches: option one or option two. Now for nesting, I'm just going to put in sort of a grocery list here of some random items, and then let's paste in a long nested expression here.
So I've got apple followed by either juice or sauce or milk, which optionally might have shake after it, or sweet, space, followed by either peas, corn, or potatoes. So that's the regular expression. It matches everything on my shopping list except for yogurt. Now it's really a matter of taste and personal preference whether you find this simpler or if you find this simpler. Just simple, non-nested ors. I've got milk and milk shake written as two things, sweet peas, sweet corn, sweet potatoes. Each time I just write them out again.
Or you may decide that you like it somewhere in between. Maybe you want to make milk shake and have shake be optional. It really is about your personal taste and also about what you're trying to accomplish. You can see here it didn't match yogurt on my shopping list. Well, maybe that was intentional; maybe your purpose was to try and find all these other items and not some items, like yoghurt. But if what you're really trying to find was all of the words and spaces that might make up a word and you wanted to find it that way, well that's a whole lot simpler. So it really just depends on your use case. But just be careful when you're working with those nested alternations that you don't either confuse yourself or come up with some very elaborate and inefficient expression that takes forever to run.
There are currently no FAQs about Using Regular Expressions.
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.