Introduction To Regular Expressions in Python - Transcript
The document discusses regular expressions in Python. It covers importing the re module, constructing patterns using meta characters like caret and dollar sign, and using functions like search to match patterns in strings. Qualifiers like question mark, plus, and asterisk are also explained. Ranges with hyphen and matching single characters with dot are discussed. The overall goal is to learn how to validate passwords and user input with regex.
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
13 views
Introduction To Regular Expressions in Python - Transcript
The document discusses regular expressions in Python. It covers importing the re module, constructing patterns using meta characters like caret and dollar sign, and using functions like search to match patterns in strings. Qualifiers like question mark, plus, and asterisk are also explained. Ranges with hyphen and matching single characters with dot are discussed. The overall goal is to learn how to validate passwords and user input with regex.
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4
Welcome to this project.
Regular expressions in python.
So in this project we're going to learn how to construct, regulates patterns validate passwords and user input in wet forms and extract patterns, replace strings with rejects. So a red X. Or regular expression or pattern is a concatenation of letters, numbers or symbols to search stuff. I would say within a larger text, a text that we want to scan. So working with regular expressions in python usually requires three steps. First you import a module called R E in python. Then you construct the pattern with various symbols and characters. And then third you use the ari function to search, replace or match the patterns to a strength that we want to skin. So in the next two tasks, what we're gonna do is learn four rejects syntax to construct such patterns, first meta characters, seconds, qualifiers, third special sequences and fourth sets. In this project we're going to use google collab as you can see her google collab is a free hosted jupiter notebook service that doesn't require any setup and provide access to computing resources such as GPU. So it's a free product from google. And if we're working our our studio recently released their web based solution called or a Studio cloud that allows anyone to coding are using the R. Studio I. D. Directly from your browser. So if you are are you can use our studio cloud if you want to code in python with jupiter notebook, you use collab. So here what we're gonna do, we're going to click on file and then we're going to create a new notebook. And gmail is gonna ask me, Google is going to ask me to sign in to my Gmail council entering my user name and password so I'm going to pause the video and turn my username and password. Come back and start with a new notebook. Okay, so we have started a new notebook here. We can see here the name of the notebook you can click on it and change the name so we're going to call it rig X. Okay. Enter and here you can see you can add code or you can add text here. For example I click on text and I'm gonna say this is the first task of this project to learn regular expressions in python. Okay. And here I'm going to put this one as the first cell. So the first cell is a text and the second cell here is where you can enter code and the first thing we're going to do as I mentioned before is to import the module R E. So when you import about you, ari you're going to be able to use the functions in our e in the module that search extract and replace strings. There's a whole lot of functions to work with regular expressions. So that's what you do first. Every time you want to work with Red X. So I'm gonna add a text here and I'm going to say we're gonna learn meta characters. Too many characters. Mhm. And the first meta character, the two middle characters that we're going to learn is the carrot sign. And these are assigned the carrot sign is used to search a pattern at the start of a string. And the dollar sign is used to search a pattern at the end of a string. So let's go and create some code. We're gonna call S. T. For strings and we're gonna say dogs are adorable. So this is a string outdoor adorable. And we're gonna search if at the beginning of this string we have the word dogs. So we're going to add another code here. We're gonna create a pattern first. So peak. And the pattern, it's always between quotes. And we're going to say at the beginning of restraint is their dogs. And or the beginning dogs. And we're going to see add another code here. We're going to use functioning R. E. It's called search. What we're going to search. We're going to search key. So the pattern in the string as T. Okay. And if there's a match, well the search function is going to return match object. If search didn't find the pattern. P industry then it's going to return none. I run everything. And here the search returned a match object. So the match here is dog. So it found dogs. And here the span is a tuple in python with zero here meaning the first position of where the pattern was found. So D. Was of course the first position, so position zero in python. And the S. Was as position for. So now we're going to see if there's dogs at the end. So we're going to create another code here. We're gonna see. And we're going to create a pattern. I'm going to put this one below. We're going to create a pattern. P. two equals between coats. Bugs his dogs at the end. So here is p. two. And I'm going to research to do a search or read a search of the pattern in the strength and if there is no match, it's going to return nothing. So I'm going to play here. So it didn't return anything. It was none. If we do a print here, be sure. Okay. And we can see that the search didn't find anything. So dogs at the end, there's no dogs at the end. Okay. So this is the first to meta characters that we can use in regards constructions. We're gonna have a text here and we're gonna call this quantity fires And we're gonna learn about 320 fires your question mark, plus sign and the asterisks. So the question mark means zero or 1 occurance of the preceding element. So here I'm going to add a code here and I'm going to add to your string. S. T. Two. He crawls color here and S. T. Three equals called lure with uh with a different spelling. Okay, so we're gonna run this, we're going to add another code and we're gonna search, we're going to see that if I do this for example it means what do I want to search? I want to search colour but the EU is going to be optional. Okay, The zero or one occurance of the preceding element of the question mark. So the President element is you? So I wanna I wanna I wanna search I wanna match if there is zero or one occurance of the preceding element which is the you. So it means that normally it's going to match for S. T. To nsd three. SD three. There is no you okay in SD two there's no you but the U. is optional. If there's no you well it should return a match option. So let's try that In S. T. two. Yeah. And or control enter. In s. T. two. There's a match object. Okay? It returned color And the span is 0 - five . And if we do SD three, control enter. Okay? We can see also there's a match object with color. So the character that's just before the question mark is basically optional. zero or one occurrence of the preceding element. Now the plus sign, the plus sign is one or more occurance of the preceding element. So you need to have at least one or more occurrence of the character that precede the plus sign. So we're going to do another example here, we're going to add another code and we're going to create other strings. For example A C. Yeah. S. T. five equals a B. C. SD six equals ABC. Yeah. Okay. I run this. I add another code and now I do worry that search what is the pattern I want to search is eight be with the plus sign and see. So the plus sign. What is before the plus sign? The B. Okay. The plus sign says it needs to be one B. or more bees to return a match. If I scan s. t. four normally there's no be so it's going to return none. Which it does right? They didn't return anything. I played again. There's nothing. Okay. If I Search for s. T. five that is going to return something, it's going to return A. B. C. In February 26 A BBC. Next what we're going to find, I'm going to click on text here, we're going to learn to more meta characters for this task. Eco cruise. These two meta characters are a hyphen and adopt For period. So the hyphen means a range. So for example 0-9, 0-9 And the dot is any single character except the new nine. So in a pattern we want to find any single character Or we want to find a range 0-9 a to Z. So here in this first task we learn about two meta characters that are very easy. The carrot sign and the dollar sign to search patterns at the beginning or the end of a string. Then we learn about qualifiers with these symbols here. These are not characters anymore. These are these have special meaning in regular expressions and we're going to see later what we do if we really want the character question marco the character plus or the character as the risk. But for now a qualifier, these three qualifiers or have special meaning. And then the last meta character Is the hyphen which means a range like 0-9 or a to Z and the period or the dot is any single character except the new line. So in the next task we're going to see a little more about meta characters, about qualifiers, about sets and about special sequences. And then after that, in less than 34 and five we're going to learn about how to match, for example, passwords and user input in Web forms using Red X. Thank you very much and see you on the next desk.