Placeholder Image

字幕列表 影片播放

  • Hello, everyone.

  • In today's video, I'm gonna be talking about regular expressions and I'm gonna be going in depth, covering everything you need to know about regular expressions.

  • This includes the simple stuff such as matters, digits, selectors and so on, all the way up to more advanced features such as captured groups.

  • Negative.

  • Look ahead positive.

  • Look ahead and so on.

  • So let's get started now.

  • Welcome back to Webb.

  • Have simplified my name's Kyle and my job is to simplify the web for you so you can start building your dream project sooner.

  • So that sounds interesting.

  • Make sure you subscribe to the channel for more videos just like this one.

  • And now to get started, I'm gonna website here, which really helps with regular expressions.

  • I'm gonna link it down in the description below so you could go to the same blood site.

  • But it's really useful because it highlights inside of the texture, searching for what your regular expression actually masses and give you details on every single thing that the regular expression is doing.

  • It's great for practicing and testing out regular expressions, so I highly recommend you go down to the link in the description and follow along on this site and now to get started.

  • I first want to explain what regular expressions are.

  • And essentially a regular expression is a way to search through a string of text.

  • So we have this text in here and at the top.

  • We have a regular expression, and with that regular expression allows us to do is it allows us to search through that text in order to do things such as validation or get certain pieces of the text we can do advanced find in replace.

  • There's a ton of things that you could do with regular expression.

  • But in general, all that regular expression is doing is it allows you to search their text in a really advanced way and even group those different searches together so you could do powerful things such as finding your place with it.

  • So to get started, I kind of want to break down the format of a regular expression.

  • As you can see up here, we have our regular expression, and in the middle here we have our text were searching and regular expression is almost always gonna start with this opening slash share this forward slash, and it's going to end with a Ford Slash as well.

  • A lot of programming languages.

  • When you define regular expressions instead of defining them in sort of stream quotations, you find them inside of these Ford slashes.

  • So everything between the Ford slashes is going to be your regular expression, then also something that's a part of a regular expression is the part after the forward slash here at the end these air, the different flags.

  • And as you can see, we can access these flags up here, and the main ones that you need to know about is global.

  • This allows you to match anywhere in the string.

  • For example, if we did at here, you can see that it matched all three of the different hats.

  • That's because we have global.

  • It allows it to do multiple matches, but if we take global off, it's only going to match.

  • The first result that we find so essentially global is like match all while without global is like match one.

  • Next, we have case insensitive.

  • So, for example, if I put the with a capital T h here and then if I make this global, you can see it's matching both of these those And that's because I have case in sensitive turned on.

  • If I turn that off, it's on Lee gonna match the that is capitalized.

  • And if we changes to a lower case, it's only going to match the lower case one.

  • For the purposes of this tutorial.

  • I'm just going to leave us on the global flag with case sensitivity is that way.

  • Capitals actually make a difference.

  • And that's just because this is the most general configuration that you're going to use, one messing with regular expressions and to get started.

  • As you can see, this works very much like a normal find.

  • If I type information into here, whatever type is going to be searched for in the string down here, for example, at I can search for Fat and I'm just going to get the word fat do ran and so on, and it works just like normal finding a place which is not really that powerful.

  • But where does become powerful is when you can actually start adding in the regular expression things, and the very 1st 1 that I want to talk about is the idea of these different special characters that allow you to do certain things.

  • So, for example, we can search for the character E.

  • And you can see we get every single e inside of our search.

  • But what if we wanted to search for a string of these so multiple ease in a row?

  • We could put E and then we could put a plus.

  • And what this is saying, Is it saying match E?

  • But we want a match.

  • At least one.

  • This is one or more of the proceeding token, so we're saying Match E or more than one e.

  • So you can see we have this baby and match this e.

  • But this is actually matching both of Artie's because there's two e's in a row and the pluses just saying match as many in a row as you can but match at least one and then another character that we can do.

  • For example, we can put in a in here and we can put a question mark, and that's saying that this is optional so optionally I want to get this a character.

  • So here we have e A.

  • So it's going to get the character E as you can see in all of these, and then if there's an A, it's also going to bash it.

  • So this question mark says zero or one.

  • I like to think of it as whatever is before.

  • It is optional.

  • Sonar case are a is optional, and wherever there's an A after an E, it's matching it.

  • But in the case where there's no way after the it's just completely ignoring it, the next one I want to talk about is going to be here.

  • The star indicator and the star indicator essentially says Match zero or more.

  • So it's kind of like a combination of the question mark and the plus.

  • It's saying that it's optional, but you could match as many as possible in a row.

  • So when we can see this is if we do our e and then Starr, you can see it's matching anything in here that is an R, and then it matches all of the ease that follow it.

  • If there are any, and if not, it just matches the art itself.

  • Another special character that's really important is the period character, and the idea of the period character is that matches literally anything at all.

  • So as you can see here, we put the period and it's matching the F, the sea and the it doesn't matter.

  • It'll match anything except for a new line.

  • It won't match a new line.

  • So we have a new land right here.

  • And if, for example, we wanted to search for tea and then we want to search for a period and we want to search for another period, you can see that it's not actually wrapping the new line because period can't match a new line.

  • Also, you may notice that we want to search for a period.

  • For example, how do we find all of the periods we can use here?

  • The backslash and in the period and with the backslash allows us to do is just cancel out anything that comes after it had essentially escapes it.

  • So since we have this special character period after it, it's saying, treat this as a normal period and not as the special wild card period that matches everything.

  • And as you can see, we're able to match any of our periods.

  • And if we wanted to say what's match?

  • Any character that comes before we could just put it, period.

  • And now we're matching any character that comes before period as well as the period.

  • Another really important special character that we can actually use here is going to be the slash w.

  • And this is saying match any word character.

  • So this is going to be things like letters.

  • As you can see here, it's matching every single letter in our list.

  • Also, there's the's slash s this is going to match any form of white space that there is.

  • And the nice thing is, these have negative versions.

  • So Capital s is going to match anything that's not a white space capital.

  • W matches anything that's not a character such as these letters here.

  • And what say we wanted to capture all of the words better longer than four characters long?

  • Well, we can't use the plus.

  • We can't use the star.

  • These don't actually work, but we can do is inside of curly braces.

  • We can put a minimum and a maximum number for how large we want something.

  • So, for example, let's say we wanted to get all the words that are over four characters.

  • We could say four and this is going to get us any four digits in a row, and if we put a comma, it's going to say, Get any four digits or more.

  • If you wanted to get 4 to 5 characters, we would say 4 to 5 and is going to match any set of characters that are between four and five long and that right there is pretty much the majority of all of your counting selectors as well as your special character selectors that you have to worry about.

  • But another thing that you can do is actually do character groupings so we can get all of the characters inside of a group.

  • So, for example, let's say that we wanted to get any single word that ends in a tea, and we wanted to get a three letter word that ends in a tea, and we wanted to get only the words that start with either F or see what we could do here as inside of square brackets.

  • We put any characters that we want a match, and it's saying, match any of the characters inside of here, for example, it's going to match anything that starts with F or a C and ends with an A T.

  • But the really useful thing about these character sets, but you can actually do ranges.

  • So we could, for example, a dizzy and this is going to be all of the characters between A and C.

  • We could also do between Capital A and Capital Z, and now we have all of the characters between A and Z that are lower case and all the characters between A and Z Better upper case.

  • But I'm going to be checked inside.

  • This regular expression could do the same thing, for example, with 0 to 9 if we wanted to.

  • It's really useful because we can do these large ranges which allow us to do these really specific checks.

  • For example, we could do like a deaf, and it's going to get anything between A and F We could do a to C, and we're just gonna get cat now.

  • In that case, something else that's really useful is the ability to put things inside of a group, and that's what we use Parentheses for anything inside of the parentheses are going to be their own group, and they only act upon themselves so For example, we could search for tea or upper case T lips, upper case T.

  • And with visit Does this little pipe character here?

  • Is it Saint match Any of these?

  • So it's like a or result is saying either lower case T or upper case T.

  • And then we want to match H E afterwards.

  • So this allows us to match uppercase tea or lower case T.

  • And it's going to allow us to match it for the word, uh, and the reason we need parentheses.

  • This because if we remove these parentheses, this character here is going to check for either lower case T or the entire word, the because it's going upon the thing on the left and the thing on the right.

  • But with the parentheses, we can actually break this up into its own group.

  • So now it's on Lee acting upon the tea and the uppercase tea instead of the full word.

  • The also something that's really useful with these groups is we can actually do something things such as our matters where we could say we want a match, for example, 2 to 3, and it's going to say we want a match 2 to 3 of this character group in here.

  • So you're over Case T or upper Case T.

  • And so we can actually see this in effect by doing If we want t e and R, you can see it's going to match between two and three of these.

  • Let's just say we want to match between two and three of those and you can see it's matching over here tr ee because it's in this group.

  • There's at least three of these and then e t because there's two of them.

  • And then, for example, we could even put things after this.

  • For example, you want to match the period key and you can see is giving us the E.

  • T period at the end because it's checking between two and three of these characters up here in the beginning.

  • And then it's saying the actual period keys coming after.

  • So what?

  • The's capture groupings.

  • What we can do is we can actually specifically say we want to do special things only to those groupings, so anything that we put after these groupings are going to affect the entire grouping and not just a single character inside of that grouping as we were doing before, because, for example, we remove all this.

  • We just changed us to, for example, e of 2 to 3.

  • What this is doing is only acting upon this e.

  • And if we put for example and are in here, it's not acting upon this art.

  • All this are is completely separate from the But if we were to put it inside of a group like this.

  • Now what this is saying is any set of R E that is seen in a row 2 to 3 times.

  • So, for example, if we put our tr ee, you can say that's going to match it.

  • We put another one to match it.

  • But of course the 4th 1 will not now.

  • Another special character, which is really important to get used to, is the carrot symbol, which essentially says match the beginning off the line.

  • So, for example, we put the tea here.

  • It's going to match the beginning of our statement.

  • But if we put a capital I and the carrots symbol, you can see that it's not working, and that's because we're doing this as one whole chunk of text.

  • So this carrot is the very beginning of our entire chunk.

  • If we wanted to do it per each individual line, we would need to set the multi line flag.

  • And now that's saying that this carrot symbol is the beginning of each and every line that we have.

  • Instead of just the beginning of the actual statement as a whole.

  • I'm gonna turn that back off just so that we know that this is going to be the very beginning of our entire statement.

  • So, for example, our tea is going to match this tea here.

  • And we could put you know, for example, that and it's gonna match that a whole word, the but only going to match the the very beginning even if we made it so that it could be a lower case t by doing this so it could be either uppercase tea or lower case T.

  • You can see it's still not matching.

  • But if we remove this, you can see now it matches both of those.

  • Another thing that you can do is the dollar signs symbol on with this is saint is matched the end of our statement.

  • So, for example, at the end, we have a period and you can see it's matching this period here.

  • But of course it's not matching the period at the end of street because we would need to enable multi line.

  • So that way this works on the end of the line instead of the end of our entire statement.

  • But again, I'm going to turn that off just because I think it's easier to work with when you use the whole statement instead of multi line, at least for our examples.

  • Now that is essentially 90% of all the regular expression stuff that you're ever going to use your really only going to use thes generic ish matters as well as captured groups in order to make these complex selectors.

  • But something that you can use that really increases the ability of your using regular expressions is look ahead and look behind and to get started.

  • I'm gonna talk about a look behind first, and we're gonna talk about the positive look behind, and the way that you use a positive look behind is you need to put it inside of a group, and this group is going to start with a question mark as well is the less than sign and an equal.

  • So this is saying that we want to look behind us.

  • That's what this less than sign is.

  • And then the equal is saying it's positive, so it must match what's inside of here.

  • So, for example, what if we want to get every single word that is followed by the word the We can come in here and we can say that we want to match either a tea or uppercase tea, and we want to match H E.

  • And we can just say we want the very first character that's followed by the word the So we'll just put a period here and you can see we're getting both of the spaces that air followed by the But we're not actually getting the word the inside of our selection.

  • As you can see, it's only the period, and that's the value of these.

  • Look ahead and look behind is they allow you to look at things that either happened before or after the thing you want to capture.

  • But it doesn't actually capture or select the thing that you're looking behind.

  • We're looking ahead for, and that's super useful and I really like that about this so we can say that we want something followed by the word the We don't need to select the word the also there's the negative look behind, and this is going to be replacing this equals with an exclamation point.

  • And this is saying that we want to get anything that doesn't have the word bell before.

  • So you can see it matches every single character except for the two spaces that have the word that before it and the negative look behind is pretty much identical to the book behind.

  • That's positive, except for it's just going to invert it.

  • So your results are going to be completely inverted from the results of the nature of the positive look behind.

  • Now we can move on to the book ahead and look ahead is very similar to look behind, but we just remove the less than simple.

  • So let's come in here and we can do a look ahead.

  • That's positive first, and we want to say that we want to get anything that has an 80 after it.

  • So we want to get any character that's followed by 80 and as you can see, we get f see Andy because these are both full by the characters A and T.

  • And if we wanted to invert that, we just change his equals to an exclamation.

  • And as you can see, we're now getting everything that's not followed by 80 so everything but the F.

  • C and the heat.

  • Now that we understand all of the basics of regular expressions, what's actually look into implementing a regular expression to check for a phone number?

  • This is a great example because it can be really simple.

  • But as you start to add different types of four men into your phone number, it can get really complex and allow you to build on your regular expression skills as you go.

  • So what's coming here?

  • Replace all this with just a random phone number will just say 1234567890 So we have our area code of three digits and three more digits and then our last four digits.

  • This is going to be the easiest phone number to check for, because all we need to do is just check for nine characters in a row, so we could just say slash d which is going to be for digits.

  • This is any form of number digit and we could just say that we want to get nine of them in a row.

  • I'm sorry.

  • We need to make sure we get 10 of them in a row because is a 10 digit number, and as you can see, that's matching that perfectly.

  • But obviously there's multiple different types of phone numbers that we can search for.

  • For example, we could have the phone number 123 4567890 And we also need to be able to account for that.

  • And now I'm going to enable multi line here because now we can actually search each one of these lines individually to make sure that they all still work.

  • So now, instead of just being able to check for 10 characters in a row, we need to check for groupings.

  • So the first thing we're gonna have is a set of three characters in a row for a digit, and then we're going to have a dash.

  • But we want to make sure that dash is optional.

  • So we put the question mark next we're gonna have another three digits in a row.

  • So we're just gonna come in here, say the D and we're gonna have three more in a row and again in optional bash will put Dash the question mark to say that it's optional.

  • And then lastly, we're gonna do the same thing.

  • We're gonna have her digits and we wanna have four of them this time instead of three.

  • And there you go.

  • We now have matched of both of these different types of phone numbers.

  • Now, obviously, another way for someone to write this phone number would be 123 space for 56 space 7890 This is also common.

  • So we again need to account for that.

  • We can come in here and essentially put this inside of a character grouping, and we could just put our space inside of it.

  • So now we're saying any form of space between these, we could just do the same thing here.

  • We want to use space or dash, and now you can see we've matched both of these, which is great.

  • Now, before we get too carried away, let's say that the end result is that we want to take any phone number that were given and converted to just 10 numbers in a row with no spaces, no dashes, no parentheses.

  • What we're gonna need to do is we need to capture all of our digits so we can again use capture groups for that.

  • If you remember, when we put things inside of parentheses, it's actually going to capture those.

  • So we can put all of our digits segments inside a parentheses here and now.

  • If we come down over into the details section, you can see that it shows all of our different matches and you can see that we have matches of 123456789 as well as the different groups.

  • And if we select here, you can see it's the exact same thing.

  • It's matching 1234567890 And again, same thing right here when we click on these different results, that showing what it's matching for each one of those.

  • So we know that we're matching those correct groups and we can come in to replace, for example, and what we can do is we can say that we want to take group number one.

  • So dollar sign $1 signed two and dollar sign three That same Take group One group to and group Three and just put them one after another.

  • And this is what happens when you don't name your groups.

  • But you can't actually name your groups by putting a question mark as well as the less than and a greater than symbol.

  • And then inside of here, you put your name.

  • So for example, we can call this area code.

  • Then we can come and look instead of our details section and you can see our area code is set toe 123 We can also name these other groups if we really wanted to, but it doesn't really matter.

  • Naming them is more for convenience of use when you're actually going for finding, replace and doing other complex things like that.

  • Now let's go on and look at another example where we have 123 and then it's going to be followed by 456 and then a hyphen and 7890 So now we need to account for parentheses around the area code which again are going to be optional so we can come in here.

  • Put Apprentice E on.

  • We need to make sure that we do the backslash before because this is a special character.

  • So we have optionally one parentheses, e opening at the beginning.

  • And then here we're going to have optionally one closing parentheses just like this, and then you can see that it's working.

  • And if we look down here, our area code 123 we got 456 and then 7890 Now, one other thing to account for is if you have international numbers, you're going to have, for example, a plus one at the beginning of the number so we can have +11234567890 This is another valid number that we need to make sure we account for.

  • So what we can do is we want to check specifically for the characters of plus and one, and again, Plus is a special character.

  • So we need to make sure we escape it.

  • And now we're saying the character plus one at the beginning as well as we can have an optional space.

  • Or we can have here a hyphen just like that.

  • And we want to make sure that this is optional.

  • Now you will notice down here at the bottom are plus one also contains the space at the end.

  • And we don't want that space so we can put another grouping that wraps just around the plus one section.

  • And now we can have our group in.

  • That's just the plus one, but we also have the grouping for the plus one with space.

  • So in order to tell regular expression to not actually check for something with a group, we put question Mark Colon at the beginning of our grouping.

  • And this is going to be a non capturing group.

  • So now you can see that that plus one with space is no longer being captured.

  • And now if we go into our place weaken, come in here.

  • We have four capturing groups now so we can make sure we capture group 123 and four.

  • As you can see, it's printing out all over numbers exactly like we want them.

  • And that's all there is to regular expressions.

  • If you enjoyed this video, make sure check on my other videos linked over here and subscribe to the channel for more videos just like this.

  • Thank you very much for watching and have a good day.

Hello, everyone.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

20分鐘內學會正則表達式 (Learn Regular Expressions In 20 Minutes)

  • 7 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字