Transcript:
[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, lets. 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 user’s 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, lets. 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 lets. Make sure we print out our result at the end, lets. 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?