[5 Minute Tutorial] Regular Expressions (Regex) in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right today we're going to dive into a programming fundamental regular expressions regular expressions are commonly known as reg X are a powerful tool to find patterns and text they're basically the equivalent of using control F on a Word document except they're much more precise and powerful this can come in handy while editing code verifying a user input or scraping the web the list really goes on and that's why I want to give you a salt intro tat reg X today in this video reg X are a challenging tool to master but this tool will get you started and oriented today we'll go through a quick tutorial on how to validate emails and replace a string using reg ax and Python regular expressions and their symbols are universal across all programming languages there are many symbols that can be used to find patterns and text I'll include a link to a reg X cheat sheet in the description below as well as to the code in today's tutorial let's see how a few of these regular expressions work in real time when I press ctrl F I get the find word prompt if I press alt R I can now search using regular expressions using ctrl F like this is a good way to make sure your reg X patterns work before you try to implement them in your Python script when I type backslash lowercase D I am searching for all valid digits so any number between 0 and 9 you can see that everywhere a number occurs it's highlighted conversely when I type backslash capital D I'm now looking for the opposite or non digits now you can see everything but the numbers are highlighted it's time to jump to our first example verifying a user's email using reg X to verify users entered an email correctly as commonplace this is a very useful application because if a website has a sign in or sign up page then it's extremely important to make sure that the user is inputting a valid email the regular expressions package in python is called re so let's import our e they're packaged imported let's write the regular expression pattern we are looking for since email accounts follow a pattern we can use a combination of reg symbols to only accept correctly formatted emails we know that the first part of an email can consist of uppercase letters lowercase letters and numbers so we can specify that as such using a reg X syntax the brackets allow us to specify that we are looking for the characters in a given group this group starts at the beginning of the word and will go until we encounter an @ symbol the plus sign after the brackets means we are looking for any combinations of one or more of these characters since email addresses are issued from many domains we'll specify that we are looking for any combination of one or more upper and lowercase letters all that's left is verifying that there is a top-level domain we can look for a dot by adding backslash and period to the pattern similar to domain names we'll just look for the presence of valid characters since there are so many top-level domains using email addresses let's just account for some of the most popular ones in this example like com edu and dotnet just add the following to the pattern so it makes sure the email ends and of calm or etu or dotnet great now let's make sure this works with an if statement and the search function from re first let's get a user's input let's see if a string from our specified pattern is found in the users input by calling our e dot search we can use the pattern argument for the pattern we just wrote and the string argument for the user input you can scroll down in the kite copilot for example implementations of reg X patterns so if the pattern is found in the user input we will print out valid email otherwise we'll just print out invalid email good now let's test our script let's input a correct email great it correctly verified that email now let's give the script an invalid email perfect it detected that the email is missing the period I wanted to take a moment to tell you more about kite which is an AI coding assistant that's being used in this video ok is a free plugin for your code editor that uses machine learning to save you keystrokes while you're programming if you're using Adam s code spider pycharm sublime or vim kite will seamlessly integrate into your coding workflow kite can complete entire lines of code and has a feature called intelligent snippets that will help you fill in arguments and Method calls with variables defined earlier in their script the window you'll see on the right side of my screen throughout the video is also a kite feature called the kite copilot it automatically shows you relevant Python documentation while you type based on your cursor location the best part of kite is that it's free and you download it from the link in the description below let's look at one more application of regular expressions in this example we will be looking at how regular expressions can be used to quickly replace a specific part of a string this is extremely useful when making changes to a large database where you may have thousands of strings you need to change let's pretend that we need every phone number entered into a continuous string of numbers no hyphens but we want to keep the hyphens that are in words like before we must first write the regular expression pattern we are looking for and test it phone numbers come in a predictable format so we can write a regular expression to identify them in our database each set of parentheses resembles a group so from left to right we have groups one two and three but now we need to write what we want this pattern to turn into let's preserve the groups but remove the hyphens each backslash number represents a group so our new pattern is concatenating the three groups together without the hyphen we put the R before the string in order for it to be interpreted as a raw string let's just take input from the user like we did in the previous example and then let's use our you replace the hyphens with whitespace as seen in the kite copilot here we're using our pattern variable for the argument pattern and our new pattern variable for the argument repl and our user input variable for the argument string and let's make sure we print out our result at the end let's try this out there we go you can see that all hyphens were removed from the phone number so that was just a sample of what regular expressions can do this is a vital skill to learn in computer science not only because of its usefulness but also because it shows up in almost every language thanks for watching this video on regular expressions in Python make sure to subscribe to our channels will have more Python tips coming your way and finally don't forget to check out the kite AI autocomplete plugin the links in the description below see you next time [Music] you
Info
Channel: Kite
Views: 209,137
Rating: undefined out of 5
Keywords: regex python, python regex, python regex tutorial, regex python tutorial python regular expressions, regular expressions python, regular expression python, python regular expression tutorial, python regular expression, python regular expressions, python regular expressions tutorial, python regular expressions examples
Id: UQQsYXa1EHs
Channel Id: undefined
Length: 6min 27sec (387 seconds)
Published: Mon Feb 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.