Enroll in The Complete Python 3 Course: Beginner to Advanced!
Click here to subscribe for more videos like this!
Hey guys, what we’re going to do in this video is learn how to import different modules to a Python script and we’re going to learn about regex. So, what’s a module? Basically, it’s an external library that you can include and use in your project providing additional functionality without you having to write this additional functionality. So, for example, we are going to go ahead and “import re” which is the regex library, and that’s how you import something, just type import and the name of it. Now, re is included with Python and so there’s nothing that we need to install in order to use it. So, now the basic usage, but well let’s get into regex. So I’m going to go ahead and create a “string = “‘I AM NOT YELLING’, she said. Though we knew it not to be true.” Now basically regex is not part of Python. It’s kind of like a mini programming language that you can use in basically any programming language. So I mean you can use regex in Python, in PHP, in JavaScript, you know, Java, C++, so regex is basically a way to match certain characters and then do something based on that. Now as you can see on Wikipedia here, there’s standard libraries for .net, java, python, c++, and there’s some built-in for Pearl, JavaScript, etc. So this is not Python specific and we’re only going to cover it to a limited degree as we need it. So, in this instance we’re going to be learning a few things for in the next video. So let’s go ahead and start figuring out how to use regex. So, the first thing we’re going to do is you know if I “print(string)” you’re going to see it says ‘I AM NOT YELLING’, she said. Though we knew it to not be true.” So we’ve got capitals, lower case, we’ve got a period, comma, and quotations. So, let’s go ahead and play around with this a bit. I’m going to create a new variable called “new = re.sub ” and what we’re doing is we’re instantiating the re object that we imported at the top of the script, and we’re calling the sub or substitute function on the re object, which the sub is built-in to the re object. So just like calling any other function when you call a function on an object you need to put the parameters into the parameter list. Now, how we haven’t discussed classes and objects yet, and we’re going to get to that, but this is you know we need to know this for the sake of this video and the next one now. The three parameters that this substitute function takes is the matches that we want to make, what we want to replace them with, and then the string that we’re going to manipulate by doing this. So we’ve already got the strings stored in the variable named string, let’s go ahead and cover some matches. So let’s say we want to remove all the capital letters. So what we’re going to do is basically open and close square brackets “[ ]” now rules in regex are contained within square brackets. So if we want to remove all the capital letters I mean we could go “[ABCDEFGHI]” you know we could do it that way and list out the entire alphabet, but regex actually provides a way to not have to do that by allowing us to choose a range of letters so this is going to say any capital letter from “[A-Z]” we want to replace with nothing, and we wanted to do this on the string, string. Let’s go ahead and hit enter. Now, if I “print(new)” you’re going to see that it removed all the capital letters and left everything else intact just the way it is. So we can also do the same for lower case letters, so instead of removing all the capital letters, it’s leaving everything except the lowercase letters. Well let’s say we want to remove all the special characters. So what we do is actually we could put multiple rules inside of the square brackets. So let’s go ahead and put “new = re.sub(.,\’]’, ‘ ‘, string)” let’s hit enter, print it out, you’ll see that it removed all the punctuation from the string. Now let’s go ahead and combine this with the lowercase letters, and then additionally uppercase letters. We’re left with nothing except the spaces, can’t see them here but there are spaces here. Let’s go ahead and leave the lowercase letters just so you guys can see. There are spaces. So there’s about four right there, you know, and so we’ve got spaces. So let’s remove the spaces as well. How we’re going to do that is within here “new = re.sub(‘[.,\’A-Z+” “]’, ‘ ‘ , string)” “new = re.sub(‘[.,\’A-Z+” “]’, ‘ ‘ , string)” Now let’s go ahead and, let’s add actually, so let’s go “string = string + “6 298 – 345″ ” then let’s “print(string)”, and what you can do as well you can actually create “new = re.sub( ‘ [^0-9] ‘ , ‘ ‘ ,)” what we’re going to do here is we’re just going to tell it to remove anything except numbers, we actually need to put that in quotations, we’re going to replace anything that’s not numbers with nothing, and we’re going to perform that on the string. So if I “print(new)” you’re going to see that all that remains is the numbers, so that’s the extent to which we need to learn regex in order to do what we’re doing in the next video which is building an awesome calculator.