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 just want to make mention of some other special characters that we need to be aware of when we're working with text files. These might not be characters that you would normally think about. First, there are spaces. A space is an actual character as far as a regular expression is concerned. We saw that before when we put spaces in the word car, we saw that it no longer matched anymore. So the space is a character and if you want to match a space, well, you just type a literal space. That's all there is to it. For tabs it's not quite that simple. For tabs you use a backslash t. It's that escape character, but notice now, that escape character is giving t a special meaning.
Now it's saying, we're looking for a tab character. Instead of trying to type a tab, we just put backslash t, and that matches a tab in the document. It's a control character. There're some other control characters. For line returns it's a little tricky because there are actually three possibilities. It could be \r, \n, or \r followed by \n. r is for line return and n is for new line. So you can have different ones in a file, and a lot of times it depends on whether that file is a Windows file, a UNIX file, a Mac file, or from somewhere else.
So be aware that this matches a new line, but that you do have to worry about all three possibilities. There are some other non-printable characters. You won't come across very much, but they could show up in a document: the bell, escape, form feed, vertical tab, and then there is also ASCII or ANSI codes. These are codes that would appear in typically like a UNIX program or something like that that would control the appearance of the text terminal and the way it would look. They typically look like 0xA9-- that's what the code looks like.
The way you would access that inside a regular expression is with \x to let us know that it's a control code, followed by the A9 portion. So the 0 is always the first character of the code; it just gets replaced by that backslash and then it looks pretty similar. You're not going to run across those very often. It's really the spaces, tabs, and line returns that I want you to be aware of, and also just to make the point that putting the backslash in front of a character that's a literal character doesn't do no harm; it actually does make it look for something different.
So backslash t is not the same thing as a t. Don't think you can harmlessly throw that backslash in there. Let's try a couple of these, just with, let's try space first of all. Obviously we have cat. Let's put spaces in here. If we want to match that with cat, we saw it doesn't match, but if we put the spaces in there, the literal spaces, now it does match. It's that easy. Now it's matching that space. If I instead had something here, like an x, now it doesn't match anymore, right; it has to be that same exact character. Let's try a tab.
Let's say that we have a\tb, and then down here let's type a and hit the Tab key, followed by a b. See that it matches? cdef, see? Let's take away our tab, and let's hit a line return after the c, and now let's change this looking from c to d. It's not a tab in there anymore, now it's a new line, so n worked. r or r\n also could have potentially worked, but in this case it's a new line.
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.