تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Lookaheads Overview Example0:00

Lookaheads and lookbehinds can be a pretty powerful feature. Let me show you what we can do with this. Imagine that you have some kind of link here. It doesn't really matter, just something for the example. And then later, maybe this is like a blog post or a comment, anything like that which you may need to parse on the backend. So is the best search engine. Okay, so if we were to hunt down this string google, it's of course going to return every occurrence of those characters, especially because we have the I flag turned on.

it's of course going to return every occurrence of those characters, especially because we have the I flag turned on. If that wasn't turned on, then we wouldn't match a capital G. So always make sure that you use the I flag if the capitalization doesn't matter for you. Now though, imagine that when you are parsing this, you're not interested in every occurrence of google, you only want to find the one that precedes the closing anchor tag. Maybe you need to wrap it within a strong tag or add some styling or whatever. These sorts of things are pretty common.

Maybe you need to wrap it within a strong tag or add some styling or whatever. These sorts of things are pretty common. So how can we do this? I want to match google, but only if what comes after it is, why don't we say, an opening tag. Well, it's true, I could just tack that on right there, and sure enough, we are matching it. However, the problem is that it is now included with our match. So if we change it down here, notice that, well, we'd have to remember to put it back on.

So if we change it down here, notice that, well, we'd have to remember to put it back on. Not always the best idea. So what are some ways that we can get around this? Well, your initial instinct is that, if our match is google and then <, you should use parentheses to create a new group only for what you're interested in. So now if I hover over it, notice that group 1 equals google minus the < sign,

So now if I hover over it, notice that group 1 equals google minus the less than sign, and that's perfectly fine for matching. However, if you need to do substitution, and remember with PHP, you would use the preg_replace function for that. Well, in those situations, we still had this issue where the less than sign is missing, and we have to add it on manually, and that just feels wrong, doesn't it?

Using Positive Lookahead1:56

and we have to add it on manually, and that just feels wrong, doesn't it? So what is the solution? I really want to say match google, but only if what comes after it is equal to a less than sign. Now, the way we can do this is through what we call a positive lookahead. So don't let that be confusing. It's very simple. We are saying positive,

It's very simple. We are saying positive, meaning we must match it in order for our preceding match to take effect, and then look ahead, well, literally means look ahead to this next set of characters. So in translation, we are saying, I want you to look ahead and make sure that the set of characters I specify here exist, and if they don't, well, that means we don't have a match.

and if they don't, well, that means we don't have a match. So let's try it out. The syntax for a positive lookahead is to do ?=, and then we wrap that within parentheses to contain it. So now if we put that back on, it works. We only matched google, and we were able to change it without having to explicitly apply that less than sign. So if this looks a little tricky,

without having to explicitly apply that less than sign. So if this looks a little tricky, it's just something that you have to memorize. A ? followed by an = means a positive lookahead, and if you think about it, that's kind of readable, right? We are looking for google, and then a ?, well, think of that as asking a question, does what comes after google equal a less than sign?

Negative Lookahead Syntax3:15

well, think of that as asking a question, does what comes after google equal a less than sign? If so, we have a match. If not, we don't have a match, as we can see in these two cases. Now, as you can imagine, there's an opposite of this. That would be a negative lookahead. The only difference is, rather than using an =, we use an !.

rather than using an =, we use an !. And now notice that we match google as long as what comes after it is not a less than sign. And if we look down here, we can see that everything was substituted like we expect. All right, so that can be pretty useful, as you can imagine. However, what about the inverse? So far, we've looked at positive and negative lookaheads, but what about when you need to look behind the sequence that you're matching,

Switching to Lookbehinds3:55

So far, we've looked at positive and negative lookaheads, but what about when you need to look behind the sequence that you're matching, and you need to make sure that something either exists or does not exist? How do we do that? Well, it's just a little bit different. However, we're going to have to switch over to my editor here, and that's because JavaScript, which this website uses to process all of these regular expressions, does not support negative lookbehinds. But anyhow, I'll show you real quick.

does not support negative lookbehinds. But anyhow, I'll show you real quick. We want to use, once again, a ?, and then a <=. And if I hover over that, notice it'll give you an error because lookbehinds aren't supported in JavaScript, and therefore it can't give us an accurate reading. So why don't we switch over to an editor? In this case, something like vim is fine,

PHP Lookbehind Replacement Demo4:37

So why don't we switch over to an editor? In this case, something like vim is fine, and I've already created a little index.php file that's empty. All right, so imagine that we have some kind of string here. And for an example, how about something like, we'll have a variable. Maybe you are parsing some kind of template. So I'll say name equals Quade. And then maybe later within the template, we have my name is Quade.

And then maybe later within the template, we have my name is Quade. Okay, so our goal, for whatever reason, is to update the variable name to be something different. However, the problem is, there are multiple occurrences of the string name. So in PHP, if we tried to do a preg_replace, we will look for name, and let's replace it with something easily recognizable. How about variable?

and let's replace it with something easily recognizable. How about variable? Using the string as a base. So we'll say result, and I will var_dump the result. And because I'm using vim here, I can set up a quick mapping. How about when I hit , P, that will run PHP on the current file, and then we will hit return.

that will run PHP on the current file, and then we will hit return. Okay, now I have the easy way to test this out. , P, and sure enough, we've updated this occurrence as well as that one. So not what we want. We only want to update the variable name. So next, we could do a $. And remember, that's a special symbol when using regular expressions.

And remember, that's a special symbol when using regular expressions. So we escape it to say, no, we actually mean a $ and not the end of a string. Save it, run it again. And yes, we were able to match that one without handling the second occurrence of the string name. However, once again, we've removed the $. So like I was saying earlier,

However, once again, we've removed the $. So like I was saying earlier, you'd have to remember to add that back on, and only then would it work. So instead, rather than doing that, we will use a negative or a positive lookbehind. Here's what that looks like. I'm going to wrap it within parentheses again to specify the sequence of characters that we are looking back for.

to specify the sequence of characters that we are looking back for. And then I'll say, I want to find a $. And that's it. So now if I run this, notice that yes, we've made a replacement, and it still includes the $. But also we've ignored any other occurrence of the string name if it's not preceded by a $. So hopefully that doesn't seem too complicated.

of the string name if it's not preceded by a $. So hopefully that doesn't seem too complicated. We're just saying match this sequence of characters, but only if what comes before it equals a $. And then once again, we have the opposite. So if you want a negative lookbehind, change the = to a $. And now if I run it, we will update this occurrence while ignoring any occurrence of name if it's preceded by a $.

Lookaround Syntax Recap7:17

while ignoring any occurrence of name if it's preceded by a $. And that's all there is to it. So remember, positive lookahead is ?=, and then your string here. Next, a negative lookahead would be ?!, and then the string. And then we have the inverse, lookbehinds. So if you want to find a string before the set of characters you're matching,

So if you want to find a string before the set of characters you're matching, that's what you do. Or if you don't want to find it, then you would use the inverse. So hopefully if this sort of stuff looked confusing in the past, now, like I've said since the beginning, it's just a matter of memorization. Take five minutes and memorize how to write this stuff.

it's just a matter of memorization. Take five minutes and memorize how to write this stuff. And then in the future, if you ever need this kind of functionality, you'll know exactly how to write it.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟