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

Practical @mention linking0:00

Alright, we've dug into this a decent amount, so why don't we review a practical example that you might use in your own applications. For example, if I switch over to the Laracast forum, you can see that if I use the at symbol and then I reference a username, that automatically gets translated into a link behind the scenes. So we could use regular expressions to offer this functionality. Let's see how. Imagine that this is your message, and we want to specify that John Doe is an actual member of your site, and therefore you should be able to click on his username. So we will specify that with an at symbol. So now, why don't we hunt that down, and we will consider a couple different options.

So we will specify that with an at symbol. So now, why don't we hunt that down, and we will consider a couple different options. To start, well, you know that a period represents any character, right? And next we know that a plus symbol represents one or more of the previous character. So that means at this point, we've matched basically everything. But why don't we specify that the first character should be an at symbol. Alright, so now it looks like we're getting there, but still not quite. For example, let's do another one. John Doe, thanks for the help. Well now, clearly we can see that we haven't really helped ourselves at all.

Explaining greedy matching1:07

John Doe, thanks for the help. Well now, clearly we can see that we haven't really helped ourselves at all. Now your next thought might be to add a space here, and let's temporarily remove the comma. But this may seem a little odd. Doesn't it seem arbitrary that it matched everything but not the word help? Or shouldn't it have stopped right here, because we did get to a space? What is going on here? Well, as it turns out, regular expressions by default are what we call greedy. So here's what I mean by that. Let's go from left to right.

So here's what I mean by that. Let's go from left to right. We looked for an at symbol. We matched it. Next, we said look for one or more of any character. So we grab it, and it just continues matching as much as it possibly can until it gets to a space. Now why do we use this term greedy? Well, because it skipped over that space, and that one, and that one. It ignored all of those until it had no option but to match that final space.

Capturing groups in replace4:18

We want to replace this with a hyperlink, right? And presumably that's what you save to your database table. So I can replace this with an anchor tag like so, and now how do we grab the value or the username? Well, we could try $1 like we did in a previous lesson, but notice that's not working at all. Don't forget, you need to wrap it within parentheses. So now that creates, if I hover over this right here, a capturing group that you can then reference within your substitution. Or if you were using PHP, that would be the second argument to something like preg_replace. Then you could access it like that.

Excluding @ from capture4:49

Or if you were using PHP, that would be the second argument to something like preg_replace. Then you could access it like that. All right, so we're almost there. Finally, we just want to have it linked to our website, or if you want to be absolute, something like that. Now in this case, the fact that the @ symbol is included in the URL is intended, but if the @ symbol shouldn't be there for your domain, then just make sure that you exclude this from your capturing group. So now this first capture is equal to John Doe rather than @John Doe. Just remember though, if you do that, then you need to tack it on within the body here.

PHP preg_replace and wrap-up5:18

So now this first capture is equal to John Doe rather than @John Doe. Just remember though, if you do that, then you need to tack it on within the body here. So now this will work for anything. For example, my name is Quade, or Quade, start the reactor, or thank you for using Johnny Cab. And remember, when using PHP, if you use that preg_replace function, that will essentially be the equivalent of applying the G flag, so you don't have to repeat yourself there. All right, so that does it for this episode. You learned about greediness, which is a key component to really understanding regular expressions.

You learned about greediness, which is a key component to really understanding regular expressions. You learned about things like \W for word characters, or \S for space characters. All of these things combined is how we manage to have so much control over how we manipulate a string.

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