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 take a closer look at a special case where backreferences refer to optional expressions. There are two things here that can really trip you up that I want you to watch out for. First, let's remind ourselves how optional elements work. So let's say that we just have a simple expression that's A, question mark, B. The A is optional, because the question mark is a quantifier that says that A can occur either zero time, or one-time in the expression, and will still have a successful match. So it matches both A, B, and B. Now let's imagine if we put parentheses around the A, and the question mark, then what will get matched if we ran it against both of those strings; against A, B, and B? The answer is that captures do still occur on zero width matches.
Obviously, if we are matching it against A, B, then what it captures is A. But if we are matching it against just B, then it captures nothing; an empty string. Why is that? Because we said, look, it's acceptable for there not to be anything here. That's still a successful match, and then after that we said, and capture whatever you matched. Well, it matched zero-width. Be careful, because then your backreferences become zero-width too. So let's put a backslash one at the end of the expression, and now it matches both A, B, A, and also B. After that, let's put a C at the end, and now you'll notice that it matches A, B, A, C; that's all well and good, but it also matches just B and C. The A just dropped out of the middle.
The A became optional in between those as well, and that's something you might not expect. So just be careful about using that, and make sure that you understand what is being matched and what's being reused. Now let's go back, and let's revisit our original expression again, but this time, instead of putting our parentheses around the A and the question mark, let's put the parentheses around just the A, so that the question mark is outside the parentheses. What would it capture in both cases then? It would still match both A, B, and B, but what would it capture in each of those cases? If we are looking at the first one, A, B, it's still going to capture A. But it's the second case when it doesn't exist at all; in that case it does not capture anything.
In the second case, the element is not optional, but the group is optional, and therefore the capture is optional. That's how I keep the two apart; by looking at where that question mark is placed, and asking myself, is the group, and therefore the capture, going to be optional or not? But the larger point here is to take extreme care whenever you start making backreferences to expressions that have optional elements in them.
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.