Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this chapter, we're going to be looking at anchored expressions, and we'll start out by looking at the most common kind of anchors, which are start and end anchors. The metacharacters we're going to need for this are the caret, the dollar sign, or the backslash A, and the backslash Z. Notice that the caret and the A, and the dollar sign and the Z, have very similar meanings. The only difference is about how it handles the difference between a string and a line. We'll talk more about that distinction in the next movie where we talk about multi-line mode. For now, let's just work with strings, and assume that they're basically the same.
Notice also that this is the second meaning we have for the caret. The first time we saw the caret was as a metacharacter, where it represented the negative character set, which is used only when it's the first character inside the square brackets. Here, it will have the anchor meaning when it's the first character in the regular expression. The dollar sign will have its meaning when it's the end of the regular expression, and the same for the A, and the Z. One important point about all anchors is that anchors refer to a position; not to an actual character.
We say that they are zero-width. Other expressions we've been working with have a width to them, because they refer to characters, and the regular expression engine expects them to match a certain number of characters. Here, instead what we're telling the regular expression engine is where to expect those characters to occur. Let's say that we have A, P, P, L, E, and in front of it we have the caret. What we're saying to our regex engine is, I'm not interested in you finding apple anywhere in the string. I'm only interested in it if you can find it at the beginning of the string; if it's the very first thing.
I'm requiring it to be in that position. It's the position; not an actual character. The second example there has the Dollar sign at the end, which says, I'm only interested in apple if it occurs at the end of the string. If it has apple somewhere in the middle, I don't care about it; it's not a match. Or, in the last case, we can put both and say, I've fully defined everything that ought to be in the string. If apple is not both the beginning and the end, then you don't have a match. So if we had a line that said apple sauce, then the first one would match, the second and the third one would not.
The caret and the dollar sign are definitely more universally accepted. I also find them a little easier to read. If you look at the examples there, where you've got a capital A sitting right next your words, I find that that's a little harder to read than the same version using the caret and the dollar sign. Let's try some examples. So let's start out with a simple phrase here: Mr. Smith went to Washington. For a regular expression to start with, let's just do capital A to Z as a character set. So you notice it found three of them; it found every capital letter that is in there.
But what if we really just want the first one? If we want the one at the beginning of the line, we put the caret in front. That essentially says, the only one that counts as a match is if the position of it is the beginning of the string. Notice that that's not the same thing as if we had a caret inside here; that's the negative character class in that case. Here it's the first character, and that let's us know it has to be at the beginning of the string. Let's try the opposite. Let's try a backslash, period, for a literal period. Now you see it found two of them.
Well, what if really want just the Period that's the end of the string? That's really what we're looking for is that tail end; we can use the Dollar sign at the end. Now it only finds one. It still parses along the whole string. When it gets to the first period after Mr, it then says, oh, is this the end of the string? No, it's not the end of the string. Therefore I don't have a match; better keep looking. And it keeps plowing its way along the string until it gets to the end, to the last period, and it says, is this one the end of the string? Yes, it is, and therefore we have a match. Let's try and write one real quick that would match everything here.
Let's do a dollar sign; let's say the first thing has to be a capital letter. Then after that, we'll make another character set that's a little more permissive. We'll put A to Z, uppercase or lowercase, it can also have a hyphen in it, and a period, and a space in it, and it can be repeated. So now we've written an expression that matches our entire string, and it has to match the full string from beginning to end, because we have those anchors on either side saying, this expression must match the whole thing. If, for example, I took out the period here, well now it doesn't match anymore, because it doesn't match this period here.
It doesn't match a part of it; it doesn't say I'm going to match a little bit. If I take that out, now it does match that little bit. I'm saying it has to match the entire string. Frequently, you use this type of full- string matching if you want to ensure that the content matches, and exactly matches. For example, let's say we were trying to do some kind of e-mail matching. We wanted to say firstname.lastname@example.org. We want to write a regular expression that would match that from start to finish. I'm going to write a really, really simple one here. Let's just say any word character, followed by an at sign, followed by any word character, followed by a literal period, and then A to Z, three times.
And that's not going to match every e-mail address, but it's going to match a lot of them. It's good enough for our purposes. So, you can see it matches. But, for example, if I do comma, somebody@ somewhere.com, now I don't have a match. Now it doesn't match the string exactly from beginning to end. Now, if you were working with data where you actually knew you had two e-mail addresses in there, and you wanted to grab the first one, you could also use these anchors to make sure you got the first one, or make sure you grab the last one, and you could take those and do something with them.
But if we're really talking about matching -- this full matching -- then this makes sure that it matches exactly this expression, and nothing else. Let me show you another example. Let's try a find white space. Let's say we've got couple of spaces here; It was a dark and stormy night. If we want to find the leading white space -- white space that's the beginning -- maybe we want to be able to replace that; we'll learn how to do that later. Inside our character set repeated, you can say anything that's a tab or a space, and that will target anything that's a tab or a space at the beginning of our line.
And then we could potentially replace it, or do something else with it; turn it into tabs, or turn it into spaces. We could do the same thing at the end by using the dollar sign, removing that, and let's say then we have, and they lived happily ever after, period, space, space, space, space, space. See? It finds all that trailing white space at the end. So that's the fundamentals of working with these anchor metacharacters. They're pretty straightforward, but pretty powerful at the same time. In the next movie, we'll talk about how these anchor expressions work when you have line breaks in your text.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104518 Viewers
56 Video lessons · 116436 Viewers
71 Video lessons · 85645 Viewers
131 Video lessons · 40992 Viewers
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.