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 we're going to talk about how you handle metacharacters inside character sets. As a general rule, you don't need to do anything: a metacharacter inside a character set is already escaped; you do not need to escape them again. They no longer have their metacharacter meaning. So for example, if we were looking for H and then a character set that had abc.xyz followed by a literal T, that would match hat and h.t, but not hot, because the dot that's inside the character set is not a wildcard. It does not have its metacharacter meaning; it is a literal dot. See where that's true? Now there are some exceptions to this, and they make sense, that the metacharacters that have to do with character sets: the closing square bracket, the dash or range character, the caret or the negation character, and then the backslash, which of course we're using to do escaping.
These characters typically do need to be escaped. The opening square bracket, usually you don't need to. The reason why you need closing one is because if you think about it, imagine in that example up there, if instead of the dot, imagine if I had a square bracket. Well, then it would say, oh the character set is defined by ABC, because I've hit another square bracket. If we escape it then it say, ah! This is a literal square bracket, not the end of my character set, and it keeps on going until it gets to the actual square bracket that closes out the set. Let's take a look at some examples.
So for example, let's say we were looking for var and then a number in either parentheses or inside square brackets. So we've got three character sets that we're looking for. Let me highlight them so it's a little clearer. The first character set there is in orange, the next one is in black, and the next one is in orange. Notice in the second character set I escaped the square bracket. And if you think about it, stop and look at it, you'll see that character set would have said, oh, open square bracket followed immediately by close square bracket, I guess I'm done. So we have to say no, no, no, we want a literal square bracket and then keep going from there. I didn't need to do that in the first one.
Now you may not always have to escape these; each regular expression engine handles it just a little bit differently. So for example, in a lot of engines if we were looking for a date where the separator between the year, the month, and the date was either a hyphen or forward slash, a lot of regular expression engines would say, oh that's not the range character. I know that's not the range character, because there's nothing in front of it, so it must be a literal hyphen, so you may not need to escape it. However, if we had a number in front of it, let's say we were looking for file and then we had 0, hyphen, backslash, or underscore as the character set, well then we would need to, most likely, because it would say oh 0 all the way up to the backslash. Let me try and give you all of those characters in that range.
Okay, so typically in that case you would need to. Let's try these out. So to start with, let's just enter our regular expression. It's kind of a silly one, but it makes the point of XYZ, and T, and let's see. It matches hat, it does not match hot, but it does match h.t. See, so it's automatically escaped. There is no need for us to put this in there to escape it again. It's not the end of the world if you do; it actually doesn't ruin anything, because it just escapes something that's already escaped, but there's no need to do it. Let's try another one. Let's say we're looking for var3 or var4, right--it's going to be one of those two formats.
So we're looking for var, and then we know we'll need a character set--we know we're going to have 0-9--and then we know we're going to need another character set. That gives us a place to sort of fill this in. So let's say, first of all, let's put the round parenthesis in there first. Okay, so now we've match the first one. Now let's also include in the character set the second one, and let's put this right here. Now in this case it did match it. If I put it here, now notice it did not match it. So you can see how it handled it just a little bit differently. It said oh, well if it's the second character then it probably isn't closing up the character set, but if it's down here, well then I don't know the difference and I can't tell.
So you can see they're all just ever so slightly different; instead we need to escape it like that. That's the proper way to handle it so that it's really unambiguous, and all regex engines should handle it the same way in that case. Let's try our last example where we had a file--actually let's go down here first and let's put in file01 and file-1, file\1, and file_1. All right, so what we want to do is write a regular expression that will help us to find this. So what could those possibilities be? Well, there is a 0, dash, backslash, and underscore.
So notice it didn't find the one that's here, because it thinks this is a range, so it didn't find it okay. Everything else sort of worked out, but we need to escape that first. Now let's escape that. Now notice another one dropped off. We've now correctly defined this one, but now it thinks this one is an escape, that we are escaping our underscore. That's not what we mean, so we need to escape that as well. And now we've escaped the two characters that matter and it finds all four of them. So again, as a general rule, you don't need to escape any metacharacters except the metacharacters that have to do with character sets, and those you typically should do, just to make sure that it's unambiguous.
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.