 Hello everybody and welcome back to the regular Expression Python series. In this lesson we're going to be taking a look at Regex character classes. Now these character classes are just going to make it a little bit easier to identify specific patterns within our string or text that we're going to be working with. At the very bottom you should actually recognize some of these patterns like with these brackets A through Z or A through Z. This is actually very similar to using something like the backslash capital D. And so there are ways you can shorten and make this a little bit easier as well as look for certain unique things. Now one thing to mention really quickly is there is one on here and I just could not find a good visual for this. But there is another one on here called backslash w which stands for boundary. We'll also look at that in this lesson even though I couldn't find anywhere that talked about it. But if we look really quickly we have backslash D which matches digits. Usually the capital letter is the opposite of that. So then we have capital D which matches any non-digit. Backslash W matches any alpha numeric character and then the backslash W or capital W is the opposite of that. Backslash S stands for matching white space and then capital S is non-white space. So we're going to be taking a look at all these and how we can use them. Let's scroll right down here. And let's start off by creating our quote or our string. We'll call this quote in this lesson and we can say my name is Neo. My phone my phone number is random numbers. There we go and that's one too many in here. Go and then we'll say my email is the matrix man at gmail.com. Neo uses gmail I'm pretty sure. So let's go ahead and create this and let's see how we can use these character classes. So what we're going to do is re.find all and again we're using our quote. So we're just going to specify that. Now let's start off with probably the easiest or the simplest one which is backslash D. This stands for digits. So we're looking for the digits which are all right here. So go ahead and run this and of course I need to actually import a regular expression. So let me put this up here import regular expression run our quote again. There we go. So now we have all of our numbers all broken out. Now what if I wanted to look for more digits. I could use these squiggly brackets and I could say I want to look for three digits. Now let's run this. Now we're grouping the digits based off of three. We're looking for three digits in a row. So now we have five three four right here three four two right here but then this has one two three four there's four digits here but it only takes the first three. This would be extremely similar. If we did like this and we said zero dash nine and if we run this we get this exact same output but this one's just a little bit longer and we can go ahead and specify other things in this as well. I personally have found that when I'm working with digits I'm looking for all the numbers so I tend to find myself using this one quite a bit more than using the brackets with zero dash nine in it but if we took this very similarly and instead of the lowercase d we did a capital D if we run this it's going to take everything that is not a number so all of the non-numeric numbers even the periods and the spaces and all that stuff and the dashes right here in between the numbers so it's taking literally every single thing except for the numbers it's the opposite of the lowercase d. Now let's copy this and we're just going to go right down here and we're going to take a look at backslash w and again this is our alphanumeric character so if we go down here say re.findall and we're say comma quote and when we do a backslash w we're going to almost get everything except we're really taking out all the fillers all the periods all the white spaces all the dashes all of these things are taken out now what if we change this to a capital W we'll get the exact opposite so now we're taking all of those filler things we're taking all the spaces all the dashes the periods the at signs we're taking all of those things which is the exact opposite of what we get with the lowercase w now let's go ahead and copy this quote we'll come down once again now we're going to be taking a look at the backslash s now lowercase s is going to match any white space characters like spaces tabs new lines or things like that if I say re.findall become a quote we're just going to do backslash s keep it really really simple so backslash s now it's going to take the white spaces which is spaces tabs or new lines it's not going to take things like periods it's not going to take things like dashes or at signs it's just looking mostly at white space now if we do a capital s and just like this we're looking at everything but the white space now it's every single character except things like spaces or tabs or enter which is the backslash n or new line so now it's the exact opposite that's all the backslash lowercase s and capital s that's all that is now the next one that we're going to take a look at is going to be was going to be backslash b which is boundary and I think that one's really interesting but before I do that I am going to show you too that I almost never ever use almost ever but I'm going to show it to you anyways just so that you know I feel like I've showed you everything I'm not withholding things from you um and what we're going to do is read dot find all and actually put right here we'll look at the backslash a and backslash z we're doing read dot find all we'll do comma quote and what we're going to do going to use our quotes and say backslash a backslash a and z are kind of like a carrot and a dollar sign in the meta characters you're looking at the beginning and the end of a string now if I just run this I'm going to get nothing but what if I looked for a bracket and I did like e to z or a dash z a dash z and let's run this now I was able to find the very first character because we were looking at capital M so we found that but if we did z now that's not going to work much like the dollar sign this needs to go at the end now if we want to look at the end so let's run this now we found the M at the very end these two I almost never use unlike when you're using a carrot and a dollar sign I feel like the A and the Z are just pretty much they're redundant they do the exact same thing as something else in the exact same way except you use backslash z instead of just doing something like a dollar sign which gives you the same output and the dollar sign I think is just easier to spot easier to visualize and whenever I use it in my actual job that's how everyone else did it as well no one ever used those backslash z's or a's so again just wanted to show it to you but not something that I really actually use if I'm being honest now let's go on to B which stands for boundary now for this one we are going to actually use a different quote I will keep it on the similar theme we'll say I love the matrix is matrix love a word question mark now this makes absolutely no sense but it'll make sense in just a second let's go ahead and bring this down and we're gonna get rid of all of this and if we want to search for just the matrix we can do that let's go ahead and search and we're able to find both but we can use these lowercase b's as boundary points so we're gonna say backslash b and then backslash b so we're looking for a boundary on the left hand side a boundary on the right hand side kind of like white space and what we can do is look for that we're not getting an output let's actually all right here there we go so this represents raw string so now we're looking at a raw string literal that's what this is called I'm not going to go into what all that means but basically it'll change this from just a regular string into a raw string literal which sometimes definitely can matter in regular expression and we'll look at that in the next lesson but just wanted to show that to you so putting that quick fix but we're looking at boundaries so we have a boundary on the left hand side we have a boundary on the right hand side it's only going to pick up this matrix because this has l o v e on the right hand side has a boundary on the left it does not have a boundary on the right now if I get rid of this second boundary right here I run this we'll get both because this matrix has a boundary on the left and this matrix also has a boundary on the left now what if I wanted to use the capital b let's do the capital b let's run this neither of these work and that's because we're looking for a non boundary and so we're looking for matrix but on the left hand side we have no boundary and that could either be a period or it could be another letter so if you just got rid of this right here we're looking for a matrix but with a non boundary which should be that m we should be able to find both of those so those are our character classes and the meta characters and the character classes separately when you're learning them in two different lessons they may not fully 100 percent make sense and I try to do my best to visually see it but where it really shines is when you're looking for actual patterns so in the next lesson we'll be looking at use cases so real use cases where you can use all these things in combination to specify very specific patterns and that's really the power of regular expression so again this is the end of the character classes lesson thank you so much for watching if you like this video be sure to like and subscribe below and I'll see you in the next lesson