Series Introduction0:00
In this little series, we'll be talking all about regular expressions, something that, at least for the initial part of my career, completely baffled me. Whenever I actually had to write a regular expression, I never actually took the time to learn about the various symbols, so it always felt like I was sort of in the dark without a flashlight, and I just had no idea what I was doing. But luckily, as intimidating as it may be, this, like anything else, is something that can be learned. So in a handful of videos here, if you're interested, why don't you come along and learn something new. I promise, at the end of the day, this just sort of comes down to memorization. In the same way that you know what 5 times 5 is, without actually having to perform the calculation in your head, the same will be true for this. You just need
Template Variable Replacement0:38
memorization. In the same way that you know what 5 times 5 is, without actually having to perform the calculation in your head, the same will be true for this. You just need to memorize some stuff, and you'll be good to go. Alright, so to finish up this opening lesson, let me just give you an example of some stuff you can do. As our first example, imagine that you want to set up a very makeshift template compiler. Maybe something where you quickly want to load a view, do some quick dynamic replacement, and be done with it. For example, maybe like my name is name. So maybe for your little template, you decide that the syntax will be a $ sign, the name of the variable, and then a closing $ sign. And this will represent a dynamic variable in your view. Well, how might we use a regular expression to perform this
name of the variable, and then a closing $ sign. And this will represent a dynamic variable in your view. Well, how might we use a regular expression to perform this replacement? Well, it's possible that you won't need it. For example, if this is the only thing you have, then in PHP, you can just stick with string_replace. Look for that, replace it with maybe the username from your database, and then feed in the text. And that would be fine. If you can get away with this, that's very, very fast. However, sometimes you need to be more dynamic, where you can't actually give it the name of the variable. You basically want to say, look for a $ sign, and then anything, and then another $ sign. So when we think in that way, that's sort of what a regular expression allows us to do. It allows us to match any sequence of characters that we specify. In our case,
Using Regexr and Flags1:58
dollar sign. So when we think in that way, that's sort of what a regular expression allows us to do. It allows us to match any sequence of characters that we specify. In our case, let's try this out. So to begin, right up here, we have our regular expression. And if you go to regexr.com, this is far and away the best online tool you can use. You can even hover over these symbols to figure out exactly what they mean. For now, just think of G as a flag, and that's to perform a global search. And then we have these two slashes, and think of that as your way to specify what the beginning and the end of your regular expression is. It's just a delimiter. All right, so to start, what if we just tried to match it directly? Well, notice that nothing is highlighted here. So what's going on? Well, let's just try name. All right, well, that matches that. However, notice it didn't
to match it directly? Well, notice that nothing is highlighted here. So what's going on? Well, let's just try name. All right, well, that matches that. However, notice it didn't match this in here. Well, that's because our regular expression didn't specify that. If you want to say that it can be capital or lowercase, then you can always provide a flag, like so. So if we use the i flag, now when you write this, you're saying a capital N or a lowercase n, followed by a capital A or a lowercase a. And notice how I said followed by. That's the way you need to think of these regular expressions. We're going left from right, and we're saying look for an N, followed by an A, followed by an M, followed by an E. But for now, let's turn off that flag here. Okay, so why aren't we able to match this sequence, like so? Well, that's because for
Escaping and preg_replace3:24
followed by an E. But for now, let's turn off that flag here. Okay, so why aren't we able to match this sequence, like so? Well, that's because for a regular expression, a $ sign has a special meaning. If we hover over it, it will match the end of your string. So basically what we're saying is look for the end of a string and then name and then an end of a string. So what if we want to say, no, I actually mean a $ sign? Well, like we do in PHP and anything else, we just want to escape it. Now we're saying, no, I actually mean a $ sign, and it matches. Now if you wanted to perform a replacement, you could do something like this. Now when it comes to PHP, this might take the form of something like this: preg_replace, we provide our delimiters and then paste this in. We replace it with Jeffrey Way, and then we feed
it comes to PHP, this might take the form of something like this. preg_replace, we provide our delimiters and then paste this in. We replace it with Jeffrey Way, and then we feed in our string. And this function right here will give us the same output, which we could then save. All right, but like I said, we're still hard coding name in. So what if we want to say, just look for anything between those dollar signs? Well, that's where it gets really fun with regular expressions. For example, I could begin by just saying, look for any character, and then we want one or more of them. So now you're starting to see if you've never used a regular expression before and you see it, it really looks like a different language. But once you memorize what each of these means, it becomes so much simpler. Now I can look at this and see, okay, I want a $ sign, and then it looks for one or
language. But once you memorize what each of these means, it becomes so much simpler. Now I can look at this and see, okay, I want a $ sign, and then it looks for one or more characters and then another $ sign. So how does this plus work? Is that connected to the period? And the answer is yes. When we have things like plus or star, all of these things that I'll teach you about, they are referring to the previous character. So when I have plus, it's saying, look for one or more of the previous symbol. And the previous symbol here is saying any character. So this translates to one or more of any character. To give you another example, let's just say, hi, and then a bunch of the letter A. Well, now if we were to update this, we could say A+, and now notice that it matches all of them. However, if I don't do a plus, notice how there's little spaces between each of
now if we were to update this, we could say A+, and now notice that it matches all of them. However, if I don't do a plus, notice how there's little spaces between each of these As? That's because each of them has its own individual match. And that's because we have global turned on. If I turn that off, notice it will only find the first A. All right, let's bring it back. So if I undo this just a few clicks, we are back to where we were, and we can do the same replacement. Or maybe you decide that the rules for your template is that it should be within dollar signs, but you do need these letters to be capital. Well, for those, we can use something a little different, like this, A-Z. All right, remember, you can hover over any of these symbols to figure out exactly what they mean. In this case, when we use braces, we are saying we want to match anything within
All right, remember, you can hover over any of these symbols to figure out exactly what they mean. In this case, when we use braces, we are saying we want to match anything within here. Now, this could be as simple as single letters. For example, if I did E, N, A, M, and then do a plus, notice that we still get our match. And that's because I'm saying I want a $ sign and then either a capital E or N or A or M. Notice that when we have a character set, I can use that keyword or. So I'm not saying look for E, then N, then A. I'm saying E or N or A or M. But then we have this plus again, which is saying as many times as you can, I want you to match one of these letters. However, there is a condition to that. That condition is that I need to find a $ sign as well. So that means if we were to remove that closing $ sign, it wouldn't match because this regular expression doesn't
That condition is that I need to find a $ sign as well. So that means if we were to remove that closing $ sign, it wouldn't match because this regular expression doesn't work. We found a lot of it. For example, this part all matches. But remember, a regular expression is going left to right and it needs to match if you specify that. So as soon as we add a $ sign, it went, nope, I couldn't find that. So there's no match here. So let's return that back on. So now you can see this stuff looks confusing. It looks foreign. However, it's just a matter of knowing what these symbols mean. Now, in this case, I could just do A-Z. Now I'm saying match any capital letter, one or more of them, and make sure it's within a $ sign. Here's another example. Imagine that you have some tweet. Visit lyricast.com to view my work. Okay, so now maybe in your project, you want to search through all of the
Matching URLs in Text7:53
dollar sign. Here's another example. Imagine that you have some tweet. Visit lyricast.com to view my work. Okay, so now maybe in your project, you want to search through all of the tweets that you pull in from the Twitter API and make sure that anything that looks like a link may be clicked. So let's do that. And this will be a very simplified example. Not quite perfect, but enough to get the idea across. So maybe we want to look for any secure URLs. But now, how can I say, well, basically anything. And the truth is, if you want to match a URL, it's actually pretty difficult. However, why don't we just start out by saying, let's look for anything that's not a space. So what we're doing here is saying, once again, match an H, then a T, then a T, keep going. And finally, we want to say, we don't really care what you match. I just want to match as much as I possibly can until I get to a space.
match an H, then a T, then a T, keep going. And finally, we want to say, we don't really care what you match. I just want to match as much as I possibly can until I get to a space. And when I get to that space, presumably we are done with the URL. Well, maybe we can once again reference a character set. But this time, rather than saying an N or an E or an A, I want to say anything that is not that. And if we precede it with a caret here, that's our way of saying the inverse. So now we'd be matching anything that is not a capital N or E or A. In our case, we want a space. So I can either reference that by just entering a space, or I can use the representation for that, which is a \s. So now I want you to notice that it picked up this full section right here, but then it matched the L. And that's because that fit this right here. Remember, it won't just continue on indefinitely. You have to tell it how many times
this full section right here, but then it matched the L. And that's because that fit this right here. Remember, it won't just continue on indefinitely. You have to tell it how many times you want to do this. For example, I could say, I want you to match three of this preceding sequence here. So that will mean it will match anything that's not a space three times. Or I could say, I want three matches or up to five. So you see how much flexibility we have here. And that's why these regular expressions can sometimes get confusing. Like I said, once again, it's just a matter of memorization. In our case, I just want to bring it back to a plus. Grab as much as you can until you get to a space. Now we can wrap that within a hyperlink. But at this point, how exactly do we latch onto this URL? Well, we can use these special symbols like $1, and that would refer to the first match. But in this case, notice that it's actually literally echoing $1.
Capture Groups and Optionality10:18
exactly do we latch onto this URL? Well, we can use these special symbols like $1, and that would refer to the first match. But in this case, notice that it's actually literally echoing $1. And that's because we haven't performed any matches. So watch what happens if I wrap everything within parens. Well, now think of that as our way of saying, I want to match anything between these parentheses. And when you do, it will be contained within this first match, which we represent with $1. But what if you need more? For example, we now have this full match here, which we refer to as a group. But now we want another one specifically for the URL itself. Within here, let's grab all of that. So now we can see we have two different matches, group one and group two. That means I can paste this in as the text, and there you go. But like I've been saying, regular expressions are really tricky because you have to consider
group one and group two. That means I can paste this in as the text, and there you go. But like I've been saying, regular expressions are really tricky because you have to consider all of the various possibilities. For example, well, what if the URL isn't secure? It's just HTTP. Well, now we've broken everything. So maybe we could say we want HTTP as, but the S is optional. Well, that's an easy one: ?. And now, very much like a plus says one or more of the preceding character, the question mark says for the preceding character, that's optional. You can match it or you cannot. I don't care either way. So now, once again, we're back to matching this. Or maybe it's some kind of FTP link. Well, now, once again, we're back to that same thing. So maybe what you could do is say I want to look for HT or F. Well, we can represent that with a brace. However, even though this looks like it works, it's not quite what we want. Yes, it's
So maybe what you could do is say I want to look for HT or F. Well, we can represent that with a brace. However, even though this looks like it works, it's not quite what we want. Yes, it's matching this, but only because we specified that. So what we said here is we want to match HT or this sequence right here. That means if I bring this back, notice that, yes, it matched HT and it was done. So obviously, that's not quite what we want. What I really wanted to say was match HT or F. So if we enclose that within parentheses, now this will work and this one will as well. But once again, just illustrating how confusing regular expressions can be, because we wrap that within parentheses, we have a new group. So what we can often do is things like this, ? :, which is our way of saying, yes, I'm wrapping this within parentheses, but I have no real need to capture what's in here as a group. So that's why we call that
like this, ? :, which is our way of saying, yes, I'm wrapping this within parentheses, but I have no real need to capture what's in here as a group. So that's why we call that a non-capturing group. All right, so with this, if it makes you feel better, I fully expect all of this to be incredibly confusing for you at this point. This episode was very much intended to give you a crash course bird's eye view of how some of these symbols work. In the next episodes, we'll rewind the clock, slow down, and really dig in.
