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.
Now that we've seen our first metacharacter, we've also seen one of the problems that comes up with metacharacters, which is, if a metacharacter has its special meaning most of the time, what happens when we want its literal meaning, or its alternate meaning? So in the case of the period that we saw with 9.00. we didn't want it to be a wildcard; what we wanted was for it to actually be a literal period, a decimal. Well, to do that, we use another metacharacter, which is the backslash character. What it says is escape the next character; give it a different meaning or an alternate meaning.
So in the case of our period, if you wanted to match a literal period, or decimal, you would use a slash dot. So in our example with 9.00, you put the backslash in front of the dot/ Now we've escaped it. Now it matches 9.00, but it does not match 9500 or 9-00. See how that works? It's not just for the period; it's for any metacharacter that we have. If we even have a backslash and we want the literal backslash, well, then we escape it with a backslash. The first one has its metacharacter meaning saying to escape the next character. Then the next one doesn't have its metacharacter meaning anymore; it's its literal meaning, which is the actual backslash.
Now this is only for metacharacters that you're going to escape them. Literal characters should never be escaped, because in the case of literal characters a lot of times it gives them meaning. Backslash in front of a D means something different than a literal D. So be careful about escaping things that aren't metacharacters. An important point is that quotation marks are not metacharacters; they do not need to be escaped. Now if you're used to working in a programming language, you may be used to putting, let's say, double quotes around the outside of a string and then inside the string you need to escape those double quotes again, so you put a backslash in front of them.
It's a very common thing in programming. But in regular expressions, it is not a metacharacter; you do not need to escape them. All right! Let's try some of these out. Let's first try our example of 9.00, and let's put in 9.00, 9500, and 9-00. See, right now it matches all three of them. As soon as I escape that dot, now it only matches 9.00. See how that works? Let's try another one. Say we have two files--his_export.txt and her_export.txt--and what we want is to match both of them.
So we'd say h and then--so if we're looking at two of them, we'd think for this next character, we need a wildcard. There are other ways to do it, but for now we're going to use the wildcard. ..and then _export and then .txt. So you can do this and think, oh, wait a minute. This here I mean the wildcard; here I don't mean it. What I mean is the literal dot. See the difference? So here it's a wildcard, here it's not, and the highlighting helps give you that hint.
Now here, if something matched, it would still match. Let's try another one. I'll show you. Say we had resume1.txt, resume2.txt, and then the last one, let's say we have resume3_txt.zip. So let's say we're trying to write a regular expression that will match those. We want to match the first two, a .txt file, but not the third one. If we just try resume..txt, look what we've matched. We matched more than the dot.
What we want to do is escape the second one. The first one we want because we want it to pick up both the 1 and the 2 and to match both of those, but we don't wanted to match this third one, because it's not a.txt file. The last point I want to make about escaping is that you may need to also escape forward slashes as well. So here is /home/user/document.txt. That's a file path like we'd see from the command line, and if I wanted to write something that would match that, you might think, oh, well let's just basically write the same thing, but let's escape the dot.
You may also need to escape these, and the reason why is that right now I'm not typing the forward slash forward slash. Those forward slashes, remember I told you, in a lot of programming languages are the way that we denote a regular expression. If that's the case, well, then we're also going to need to escape these as well so that it knows that we're not done with our regular expression yet, right, because that second forward slash is the indicator that our regular expressions are all done. So just keep an eye on that. In this context we don't need to do it, but if we were using this inside something besides regexpal, and we had those forward slashes denoting our regex, well, then we would need to do it.
Escaping metacharacters, especially the wildcard metacharacter, is an important thing to remember. It seems simple, but it will trip you up if you're not careful.
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.