 Hello everybody and welcome back to our series on regular expression. Today we're going to be taking a look at meta characters. These meta characters are special characters just within regular expression that'll change how you actually specify what pattern you're looking for. What we have right in front of you is how you can use regular expression to match for an email. This may actually look really complex and to be fair it kind of is there's some unnecessary stuff in here that we'll actually take out because you don't really need it. But to me this looks very overwhelming when I first was learning about regular expression but now that I've learned what regular expression is and how to use it this isn't that tough and a lot of these special characters that you're seeing things like brackets or squiggly brackets periods dashes plus signs all of these things are meta characters and they all do specific things. So in this lesson we're going to walk through this meta characters and explain how to use them. So let's come right down here let's import our module so we'll say import regular expression and now we're going to be using a lot of different strings and quotes throughout this lesson because there's a lot of different use cases for these. So I'm going to kind of cater how I'm showing you these to each quote. So let's start with our very first one just call this one string you can call it quote or really whatever you'd like to call it and I'm going to say I like the mountains in the spring which is very true and we're going to come right down below what we're going to do is read dot find all and we're going to use find all for all of these. Now you can use the different ones but find all is just going to be the simplest to demonstrate all the meta characters in my opinion. So what we need to do is have a parentheses and we can go ahead and do comma string. Now we need to specify our pattern the first pattern that we're going to look at right here is the brackets. Now these brackets are going to allow you to look at a range which is very unique because if we did something like let's take out the brackets a dash m and we run it we're going to get nothing in our output because it's looking for a literal string that looks like a dash m but when we put the brackets around it so now we're looking for the characters a b c all the way through m and if we run this it gives us this list with all these lowercase characters a through m so you'll notice there's no capital I at the beginning here there's also no t so if you say l i k e and then the does not have the t that's because that's outside of this range a through m now if we wanted to get the capital letters as well like capital i we can do that and i'm actually going to put an a through z and we can also do a dash z which is uppercase so let's run this and this one's much longer so it's doing it like this but I like the mountains in the spring it spells it out the entire way now what if I just put in here some random numbers I'm gonna go ahead and run this if we scroll down we don't have that so we're only right now looking for letters but we could also say zero dash nine and now if we run this if we go to the bottom you'll notice we have those numbers I'll demonstrate this just one more time down here using the brackets we'll have a different string on this one so we'll say string is equal to I have 123456 wallas with an exclamation point and we're just gonna say re dot find all and now we're looking for a bracket of just the numbers so now we're just gonna say bracket zero dash four so we're only looking at zero through four and of course we want to look at the string so now if we run this we're not getting any of these characters that are text we're only getting the numeric zero through four we also don't get five and six because that's outside of the range that we've specified so that is brackets that's how we look for ranges let's go on to our next one and the next one that we're gonna look at is the period now the period is gonna match any single character except for a new line which is backslash n that's the only one it's not going to look at but we can say your new string we'll say string um you can see seashells by the seashore kind of like that old uh you know sally by the seashore or sells seashell I can't even remember um but it's kind of like that one and what we're gonna do is just copy this or not we don't need to write it out every single time but now we're gonna look at a period now if we just say a period like this and we run this we're gonna get every single character that's exactly what it does we're gonna take every single character in here even the spaces or periods if we have them we can put one in here or even the period at the very end so we're taking every single character typically that's not how I would use it there have been some use cases where I've done that but more often than not I'll do something like uh s dot a I'm looking for something that starts with s has one character in the middle that ends in a now this is not a real use case of course I'm I've never done this in my job but if I search for this now it's only going to return patterns that match that exact thing so it starts with s then it has a character doesn't matter what it is so if I say uh sba over here if I run this it's looking and it matches that pattern the next one I'm going to look at and we're just going to copy this because I'm going to reuse it is we can use squiggly brackets now what squiggly brackets does is that specifies the exact number of occurrences of the preceding character or group so right here we have the period character if I do a squiggly brackets I can specify that we need one and this should give us the exact same output because right here we're specifying one but down here we can specify one as well so let's run this and as you can see it's the exact same output now here I can specify two or three or four five six it really doesn't matter I could do the exact same thing up here though I could do one two three four five six seven eight nine ten all the way up to as many as I would like but it gets kind of messy that's where these squiggly brackets really shine is when you have a higher number if you're doing one two three and you want to specify here's how many values I'm looking for of this group or this character that I'm using before it I'll give you one more example of using those brackets and it's going to be pretty similar but it'll just be a different string kind of I will say string is equal to well well well if it isn't I need to use double quotes here don't mind me if it isn't will Wilmer then we're going to come up here we'll just copy this because it's very similar and what we want to do is we're going to say it starts with a capital W it ends with an L and we want two characters in between it so right here now if we didn't specify the two you might be able to get this W I L right here let's go ahead and run this really quickly so all we pick up is this first well because again we're looking at capital W and then we're also getting will right here so we're getting well and will and we didn't pick up this one right here because it has two characters then the last one has a four so the I and the L are these two characters right here and then it needs to end with an L which it doesn't these wells over here only have a lower case W so we're looking for a capital W so we don't pick up those either let's go to the next meta character and the next one we're going to look at and really kind of use these at the same time we want to look at the beginning and the end so we're actually the beginning and the end so we have the dollar sign and we have a carrot now this looks like an upper arrow but it's called a carrot the dollar sign represents the end of a string whereas the character represents the beginning of the string so we're going to come right down here I say string is equal to and we're going to say happy birthday to you happy birthday to you happy birthday dear Alex happy birthday to you said that very sad uh but I couldn't sing it in my head uh or I couldn't sing it when I was singing it loud so let's do re and I mean I don't want to I don't want to keep writing this uh let's go right here there we go so now we have our string and now we're going to use both the dollar sign and the carrot now for the carrot we're going to have our quotes and what we need to specify is the carrot and then happy that's what we're searching for at the very beginning let's go ahead and run this and of course we get a nice output of happy but what would happen if I put it at the end right here I have happy and then carrot if I run this I'm not going to get an output so for this one it definitely matters that you have it on the left hand side of whatever you're searching for and it's only going to search for the very first in your string it's not going to search at each sentence even though I have a happy multiple times here so if I search for something else like the letter or the word you which comes right over here if I search for this it's not going to find anything because this does not come at the very beginning of the string now I'm just going to copy this and instead of doing the carrot we're going to use a dollar sign now the dollar sign is going to come at the very end so now we're looking at you period so if I replace this with a dollar sign right where is that there we go if I do you dot just run this and see what happens it's not going to work just like the carrot you have to have it on the correct side with the carrot it was on the left because it's the beginning of the string and then with the dollar sign because at the end of the string we need it on the right hand side I'm going to come right over here and bring it and I'm going to do the dollar sign and if I run it you'll see that we do in fact get a match so that's the carrot and the dollar sign very simple but honestly used quite a bit so those are good ones to know how to use now let's go ahead and take a look at our next three now these three are quite similar but there are distinct differences between all of them we have the star we also have the plus sign and then we have a question mark now all of these are for matching specific occurrences of a value so if we look at this star that's going to represent zero or more occurrences so I'll even put it like this just so we you know can visually see it while we're working through these I'll do this I'll do this so this is zero or more the plus sign is one or more so this one can have zero that the plus sign has to at least have one and then this one the dollar sign is zero or one so again very very very similar but they all have their own way of handling their occurrences the star is zero or more plus is one or more and the question mark is I wrote zore but zero or one so let's see how these actually work create our string and this one's going to be interesting it says this thing called a symbol has thrice hurt me I know it's a common occurrence that's a common phrase in my household uh and then we'll copy this we're going to bring it right down here so now we have our string and now we're going to specify our pattern let's start with the star which means zero or more so let's say we're looking at ti and let's do it like this because that's a pretty common in this string then we're going to do a period and what we're looking for when we use this star it's going to affect the preceding value so again it's affecting this which says now we're looking for a character that has zero or more let's go ahead and run this now this output might look kind of strange it's literally the entire output why is this happening well because we're looking for ti which we get at the very beginning of our string but then we're looking for zero or as many as we possibly can get in this string and if we look at this it goes from s and it just keeps going these are all characters this is the more part of the zero or more and we go all the way until the end of the string now what if we went all the way to an s which is this so zero or more we might just get this or we might not let's take a look at it and see what happened so now we get a different output it goes from this thing called a thimble as now why did it stop at has and not this what we're actually looking for it is ti so we found it then what we're looking for is all the characters until the very last s so if there wasn't this s over here it would have stopped and let's actually get rid of that really quickly just get rid of that s let's run this and now it's just going to pick up this right that makes perfect sense but now we have this other s so now it's searching all the way until it finds the last s and when we run this it goes till that last s right there it's also worth noting that with this we also have the zero that's why when we get rid of this s and we're looking at just this it's able to capture it it's doing that even though we have zero characters in between the th i and the s that's because it says zero or more let's take this exact same string really quickly and all we're going to do is change it to a plus if we change this to a plus now it has to at least have one or more so now if we run this you'll notice we don't get an output that's because we have to at least have one character between th i and the s and we don't have that so now let's use the same one except now we're going to use the question mark now remember the question mark is zero or one we can't have two we can't have three we can't have four it has to only have zero or one so if we change this to a question mark now we're only looking for one or zero characters this period one or zero so now if we run this we do get this as an output much like when we did the star it was able to get zero and we were able to get this as our output as well now what if i change this and we go all the way back i change this to an e go ahead and run this now again it's going to take th i and go all the way until the final e we get the entire output but what if we look at right here and we go we change this one to an e and we're going to get the entire output because we're looking for one character or more and again we have a lot more all the way to the final e and what's going to happen if we change this to an e what it's going to look for is it's going to look for the th i and then one or zero occurrences before an e now the only one that kind of fits into this is thimble thrice starts with th r so that one really doesn't work but we have th i let's go ahead and run this now what we could do if we really wanted to get thimbles we could come over here use those squiggly brackets that we were looking at now we're looking at th i period and then we're looking at three of those so we're looking at three periods and the question mark is just going to say we at least have to have zero or one of those three periods let's go ahead and run this and now we do get the output of thimble now let's go down and we're going to take a look at our next one let's create our string first and we'll say i hate that i love balloon nice what balloon balloon animals they are beautiful and this is one of my own personal confessions if i'm being honest like that and what we're going to do is we'll come right down here we'll copy this and the next one that we're going to take a look at is this bar right here now this is above my enter key on my keyboard on my laptop it looks just like this it's just an up and down bar what this is going to say is either or so if you find this pattern or this pattern i want it returned i want to look for multiple patterns at the same time like if i looked for hate and beautiful hate is over here beautiful is over here and if i run this i get both of them what if i say uh love and love is actually in there let's do a lovely lovely is not in the string if i do lovely and beautiful it doesn't return lovely it's not in the string but beautiful was in there so either or as long as one is in there we will get the output appropriately now the very last thing that we're going to look at in this lesson is the backslash let's go ahead and create our string it's going to be really simple i like cats you like cats we all like cats now when we were working with the periods before it stands for any character so if you did read find all i don't know why i'm typing this out then we did a period then we did a string right here when we ran that it took every single character it even took the periods but what if we actually really just wanted to search for the periods well kind of like we talked about in the last lesson we can use something called a backslash and when we do that it's going to specify that this no longer is a special character this is actually called an escape sequence the escape sequence is going to take the character that comes after the backslash as a literal character it's no longer going to be a meta character or interpreted as a meta character when using regular expression now it's just looked at is a regular period so now we can search for an actual period when we're using this much like if we were searching for a question mark if we had a question mark here and we tried to look for a question mark if we run this we're going to get an error because it's actually looking for a zero or one occurrence of a specific pattern but we do a backslash question mark now we're looking for this actual literal character of this question mark right here that does come to play quite a bit if we go back and look at the example up here you'll see several of these backslashes because what's happening is is we're looking for specific things we're looking for specific values like right here we're looking for a period in an actual email address like gmail.com and so now that we know what a lot of these things actually do this makes a lot more sense we're looking for a specific pattern and we're saying plus the at sign then a specific pattern dot and then we have a few letters that we're looking for and we're limiting it to two and four so there are still things in here that we haven't learned that we're going to look at in the next lesson when we look at character classes like this backslash w these are quite helpful when we're looking for these patterns as well so we'll take a look at that in the next lesson so this has been our lesson on regex meta characters like i just said we're going to be taking a look at character classes in the next lesson if you like this video be sure to like and subscribe below and i will see you in the next