Motivating Character Sets0:00
Alright, let's talk about character sets. So imagine that you need to match any of these words right here. Well, we know the thing that's similar is the last three letters, O, R, and N. Alright, so if we type that and we have global turned on, then of course we will match those segments of each word. But we want to match the full word, right? So how can we do this? Well, from the previous lesson, you might think, well, we can use alternation. We could say, get a B, or a T, or a C. And now, yes, it looks like that worked for corn, but not for the T or the B.
We could say, get a B, or a T, or a C. And now, yes, it looks like that worked for corn, but not for the T or the B. So why is that? And remember, if you ever get confused, just go back to the regular expression and talk it out from left to right. So we are saying, I'm looking for a B, or a T, or a C, followed by an O, R, N. Notice I didn't say a B, O, R, N, or a T, O, R, N, or a C. We're looking for a B, or a T, or corn, essentially. So if you watched the previous episode, you learned that if we want to say no match any of these letters, what you might do is wrap them within parentheses.
Grouping With Parentheses1:01
So if you watched the previous episode, you learned that if we want to say no match any of these letters, what you might do is wrap them within parentheses. Now think of this as our way of isolating them. So with this modification, we are saying, look for a B, or a T, or a C, and then when you found one, look for an O, R, N. It's a subtle difference, but an important one. Now though, are there other ways that we could write this out? And the answer is yes. Let me show you character sets. Now we can create a character set by wrapping it within brackets.
Using Bracket Character Sets1:32
Let me show you character sets. Now we can create a character set by wrapping it within brackets. Now the rule is, we will search for one of what is inside these brackets. So in our case, if we put B, T, C, notice that once again, we're saying basically the same thing. Look for a B, or a T, or a C, I don't care which one, and then once you find one, look for O, R, N. So that means if we have something else, like worn, well we don't match that, because we didn't get this first part. We looked for a B, or a T, or a C, we didn't find one, so there's no match, and we don't
didn't get this first part. We looked for a B, or a T, or a C, we didn't find one, so there's no match, and we don't get anything. However, if we tack on a W there, now we've included that within our character set. Let's get rid of that for now to keep it a little simpler. But what about things like born, torn, and corn? Remember, don't forget there's a distinction between a capital B, in this case, and a lowercase b. Those are two distinct things. So you either need to be explicit, and I'll show you a number of ways to do that, but one would be to simply tack that on.
So you either need to be explicit, and I'll show you a number of ways to do that, but one would be to simply tack that on. So now you're saying look for an uppercase B, T, or C, or a lowercase B, T, or C. Or I could remove that, and like we've demonstrated, we can tack on the ignore case flag, and now that'll work as well. So to break this down even further, let's just temporarily get rid of all of this, and do a W within a character set. So let's add a bunch of Ws, and I want you to notice that there's actually 11 matches here. So it didn't merge every W into one character group.
Applying Quantifiers3:04
here. So it didn't merge every W into one character group. Each one of those is its own match. So what about in the situations where you want to say, I want to look for three Ws? For example, maybe something like a URL. Well, how might you do that? And there's a number of ways. One option is to be explicit, and we can do that by wrapping the number within braces. So just like the question mark refers back, the same would be true for the braces here. So what we are saying here is, look for a W, and by the way, if the W is the only thing
So just like the question mark refers back, the same would be true for the braces here. So what we are saying here is, look for a W, and by the way, if the W is the only thing you're looking for, well, you don't need a character set. But we may change this later, so let's tack it on. And now we're looking for three of the preceding token or character. So that means if we only have two here, there's not going to be a match. Now though, what if we want to say, well, you can match three or five? Any between that number is fine. So if this would work for your project, well, we can modify this and pass a comma. Now we're saying we need a minimum of three, but a maximum of five.
So if this would work for your project, well, we can modify this and pass a comma. Now we're saying we need a minimum of three, but a maximum of five. So any more Ws will not be highlighted here. However, on this note, notice that because we now have three, we have a new matching group there. So what if you simply want to say, match as many Ws as you can? Well, you have a couple options. You have star, and what this means is match zero or more of the preceding character. Alternatively, we have a plus, and the only difference here is that you're saying match one or more of the preceding character.
Alternatively, we have a plus, and the only difference here is that you're saying match one or more of the preceding character. It's a small distinction, but with a star, you're saying, it's okay if I don't find any Ws whatsoever. With a plus, you're saying, I need at least one W to continue this match. And then of course, you have the question mark, which means match zero or one. So I know that's confusing. Just memorize it. Take 30 seconds and memorize these. Star is zero or more, question mark is zero or one, and plus is one or more.
Take 30 seconds and memorize these. Star is zero or more, question mark is zero or one, and plus is one or more. Now back to character sets. If we bring this back to what we had before, where we said we wanted a B, T, or C, then followed by an ORN, what if we wanted to match actually the word worn, but we're not interested in any of these? Well as the first rule, remember, if you can be explicit, then do that. If you can get away with just writing worn, then that's what you should do. But most of the times you can't, and at least for our demo, let's assume we can't. So what if we want to say we want to match anything that is not these three words?
Negating Character Sets5:37
But most of the times you can't, and at least for our demo, let's assume we can't. So what if we want to say we want to match anything that is not these three words? Well, we can tackle that by preceding it with a caret. So now when we add the caret to that character set, we've reversed it, or flipped it, or negated it. Now I'm saying, I want you to match anything that is not a B, or a T, or a C. And then once you have a match, in this case the W, then try to match the O, the R, the N. So as an example, imagine that you have some site, let's just say larycast.com. And what we want to do here is, using a regular expression, figure out what the name of the site is.
Extracting Domain With Regex6:16
And what we want to do here is, using a regular expression, figure out what the name of the site is. On this note though, just remember with your programming language, there's very likely an easier way to fetch your domain name, other than using a regular expression. But it's a good exercise. So how might we do this? Well we know that it'll begin with a WWW. However, notice I added a period here. What if we switch this out to something like Q? Well, notice that still matches.
What if we switch this out to something like Q? Well notice that still matches. An exclamation matches too. A plus even matches. So what's going on? Well in the scope of a regular expression, a period has a special meaning. A period will just match anything except for a line break. So that'll match an exclamation point, or a Q, or an uppercase Q, or a space. Any of those will be included. So a period can be helpful in these situations where you don't care what you're getting,
Any of those will be included. So a period can be helpful in these situations where you don't care what you're getting, you just want to make sure that you capture it. And we'll cover that more in future lessons. But for now if we want to say literally we want a period, make sure that you escape it. Now if we swap this out, it won't work. So at this point we know that basically what is between WWW. and .com is the name of our site, right? Or at least the domain. So let's try this out.
Or at least the domain. So let's try this out. Maybe we want to match any character, but one or more of them. So what happened here? Well let's start right here on the L. We said match any character or one or more. So it matched it. And then we said one or more, so it just keeps matching. Even a period would be included, so it just keeps matching as much as it possibly can. So how might we say match as much as you can until you get to a period?
Even a period would be included, so it just keeps matching as much as it possibly can. So how might we say match as much as you can until you get to a period? Maybe that's our metric here. So instead, here's what you might do. You might say match anything you can except for a period. And then we'll add a plus to say one or more. Next I want a special group for the domain only. And that's where we can use parentheses again. So I will wrap this like so. Now you'll see if we hover over it, group number one is equal to Laracas.
So I will wrap this like so. Now you'll see if we hover over it, group number one is equal to Laracas. So maybe we could swap this out with something like google.com. Now on this note, because we captured WWW, we'd have to include that as well. But now, and we won't cover that in this lesson, but something I want you to think about is that we needed the WWW to target the URL, but I didn't actually want to capture it. So that's something to think about. Sometimes you need characters for the point of tracking down your strings, so to speak. However, you don't actually want them matched or grouped.
Sometimes you need characters for the point of tracking down your strings, so to speak. However, you don't actually want them matched or grouped. So file that away for now, and then we'll come back to it. For now, we've just demonstrated a use of a negated character set. So let's go through this again. We match WWW, and then we escape a period to match a period, and then next we use parentheses to create a group that we could then reference later within a string replacement. And within here, all of this looks like gibberish, doesn't it? But now you basically know what it means. We have a character set, and we are saying match anything that is not a period.
But now you basically know what it means. We have a character set, and we are saying match anything that is not a period. Then we put a plus after it to say match one or more of the preceding token. So match one or more of anything that is not a period. So it does that. It matches the L. All of this stuff is not a period. It keeps matching, but then it gets to this period. It fails, so it stops right there, and you get your group name. All right, so that'll do it for character sets and negated character sets. When you feel comfortable, let's move on to the next level.
All right, so that'll do it for character sets and negated character sets. When you feel comfortable, let's move on to the next level.
