 What's going on everybody welcome back to another video today We're gonna be starting our regular expression series in Python now regular expression is used to identify and specify a pattern of Characters that you're looking for in your text It sounds pretty straightforward, but the actual syntax can be pretty confusing at first But throughout this series I'll try to make it as simple as I possibly can by the end of it You should be using regular expression like a pro so without further ado let's jump onto my screen and get started All right, so the first thing that we're gonna take a look at is the RE module plus regex methods The RE module is just the module that you need to import in order to use regular expression in Python We'll try out each of these we're gonna keep it really simple and then in the next several lessons We'll look at how you can do more complex patterns to actually search so really quickly before we get started Let's just take a look at these methods We have find all which is gonna return a list of all your matches You have search which actually creates a match object So we won't actually see what we're actually returning and we'll look that in just a little bit But there's a way to access that and it's fairly simple We also have split which is gonna return a list where the string has been split on that specific pattern that you want The last one is sub so it's just a replacement. You're gonna replace a specific string with another string So let's come right down here and the first thing that we're gonna do is we're gonna import The regular expression module. So all we have to do is run this And we're gonna come right down here now What we're gonna do is just test out all these different methods and we'll do this just on a simple string for now So we'll do quote and for this variable We'll create a quote here and it's actually use double quotes for this one And what we're gonna say is we're gonna say there's only one thing. I hate more than lying skim milk Which is water that's lying about being milk And that is from Ron Swanson So let's go ahead and run this and we have our quote now So we're gonna be using this and the first one that we're gonna use is actually these search This is the one that I don't use as much I want to kind of get it out of the way before I use find all split and sub So search is gonna create that match object Let's come right here. We can see both and what we're gonna do is gonna say re so we're calling the regular expression module We're new read dot search and we're gonna search for a specific pattern now We're gonna keep it really simple like I said, we're just gonna do milk We're searching for milk and where are we searching we're searching within the quote and that's all we need Let's go ahead and run this and like I said before it's returning this match object So it's been created and it even tells us that there was a match at the 52 to 56 position And that the match was milk now notice that it only got one of the matches So we actually have milk twice in here. We have milk over here and way over here Now if we want to use this we want to see what it was we can also do group and if we run this We can see that there is milk and that's what we were searching for and that's what was put into that match object I myself don't use search a lot. Although if you're working with like a huge book and you're searching for a specific pattern This actually can be more efficient than some of these other methods that we're gonna be taking a look at But I just typically don't use this one as much, but that is how it works now. Let's look at find all Let's come right down here. Let's say re we're gonna do dot find all and again We're gonna search for milk But this time when we're looking at it, we're doing find all which is gonna find all the results for this pattern Let's go ahead and run this Now notice that this one made a list and this is extremely helpful If you want to take all these values that match a specific pattern and put it into a list this find all is Extremely helpful. That's why I personally probably like this one the most if you can have a favorite reg X method This one is probably my favorite because it puts it into a list for you It's formatted and remember right now We're doing a super super super simple just searching for a really simple string in future lessons when we look at really more advanced Patterns we'll be able to take a lot of different things and put them into this list not just you know one word like milk Something that you can use this for is finding out how many are actually in here So you can use this with the length function or the length function And if we run this we can see that we have to milk in this quote That's really something that I use it for I think is pretty helpful when I'm looking to just see how many are actually in a Quote or a text or you know, whatever I'm working with now. Let's take a look at split Let's come right down here. We're gonna say re dot split and This one is going to split on the pattern that you specify So if we do the exact same thing like we've been doing which it makes absolutely no sense But we'll do it really quickly and we're gonna run this if we do it on milk notice that milk comes says there's only one thing I hate more than lying skim milk So skim is where we're gonna stop that first string right here Then we have a comma and notice this is all in a list So we have our first value then we have a comma and then we go to our next one Which is lying about being which is right here, which is lying about being and then we have milk That's where it cuts off. So we're splitting it there as well Then we have Ron Swanson. So it's splitting it up based off of the specified value now milk makes absolutely no sense What we would want to do is split it up on something like a period Now this is a very real use case where I've taken something like a webinar or I've taken a speech And I've separated it by its actual sentences, which is something like this Now if we do a period this actually represents a meta character That's gonna be something that we take a look at in the next lesson if we run this It stands for everything. So it doesn't really work What we need to do is use a backslash period actually specify that we're looking at Periods like this the period actually stands for any character So in the next lesson we'll dive much more into that but let's split this based off of the period the actual period So we're gonna say there's only one thing I hate more than lying skim milk That's the first sentence then we have which is water That's lying about being milk and then we have Ron Swanson So we split it up based off of the period and it makes a much more logical sense when we're actually Spitting into values and putting it into our list the last one that we're gonna take a look at is Sub and this one is pretty straightforward You look for a specific pattern and you replace it with a different pattern So let's go ahead and try this we'll do re dot sub and then we're gonna specify the pattern that we're searching for So I'll say I and then what do we want to replace it with and we'll just do you we'll keep it Super simple again. We'll look at our quote and let me actually bring this down So we can see it right here. So I'm saying re dot sub. I'm replacing the I which I believe it's just here And I'm replacing it with you and let's go ahead and run this And now it says there's only one thing you hate more than lying skim milk But we could do anything and we can do multiple values. We can say milk as well Let's go ahead and replace milk with uh skim dairy I don't know. I think that makes sense. Um, so now we have dairy and dairy So we're able to replace all of them There is one more parameter in this function that we can use We can come right here and we can specify the count. So I'll just explicitly say count But you could just do the comma or I'll just say one Now if I specify one, it's only going to replace the first one. Let's go ahead and run this You'll notice that this one is dairy and this one is milk And those are all the regex methods that we're going to look at in this lesson now Like I said before These are very simple patterns that we're looking for. This is not how regex Is going to be used when you've learned everything This is just how we're using it to show these regex methods in the next two lessons We're going to be looking at meta characters and character classes And those are really going to allow you to specify what pattern you're looking for not just a hard value like milk But what pattern you're actually wanting to search for so thank you guys so much for watching. I will see you in the next video