Regular Expressions in Python

NeuralNine · Beginner ·💻 AI-Assisted Coding ·3y ago

Key Takeaways

The video covers the use of regular expressions in Python for input validation, demonstrating techniques and tools for effective string pattern matching.

Full Transcript

what is going on guys welcome back in today's video we're going to learn how to use regular expressions in python to validate input so let's get right into it [Music] all right so we're going to talk about regular expressions in python today and for those of you who don't know what regular expressions are they're essentially patterns that we can look for in strings and text so for example when a user inputs a username we have certain criteria for example the username can only consist of lowercase letters and numbers or something like that and we can use regular expressions uh to validate that we can also do the same thing with certain codes we can do the same thing with zip codes we can do the same thing with emails or credit card numbers whatever we have certain formats certain patterns that we're looking for and we can use regular expressions instead of using if statements if else statements and so on so we can use regex regular expressions to match or to find these patterns in strings and in python we can work with regular expressions by just using a core python module called re so we can say import re we don't need to install anything here in addition it's just a core python module and then we can define a simple pattern and i'm not going to teach you all of regex here because regex is quite complex it can some regex some regular expressions can be extremely complicated so we're not going to talk about all the details here i'm going to show you some basics that focuses on using them in python we're going to start with a very very simple one it's just an uppercase word and a word is defined by uh starting and then ending completely with uppercase letters so a space is not allowed and underscore is not allowed lowercase letters are not allowed numbers digits are not allowed no special characters are allowed we just want to have uppercase letters uh from beginning to end in order to specify such a pattern what we can do is we can say pattern equals re dot compile now you don't have to compile a pattern you can also just do match or search immediately without compiling a pattern first but we're going to do it with a compilation because then we can use the pattern directly for searching and matching and we don't have to specify the pattern every time so the pattern that we're going to use is going to be the following we're going to start with a simple anchor tag and the anchor tag is essentially saying that whatever the pattern is has to start at the beginning so this is the start of the string this is the beginning of the string if we don't do it we can use we can look for the pattern inside of the string anywhere but we want the whole string to be that pattern so we're going to provide an anchor tag and then we're going to say we only want to have uppercase characters and we do that by providing square brackets and then a capital a dash capital z so all the letters from a to z uh which are uppercase and then we're gonna end this with a dollar sign representing the end of the string so now we're matching strings that start uh with an uppercase character and end with an uppercase character now one thing we forgot here uh and the one thing that we forgot here is that we need to add a plus and the plus basically says that whatever we have here in this case any uppercase character has to occur at least once but can occur as many times as we like so for example the empty string would not be matched by this pattern and also this string here would not not be matched by this pattern but the string j for example would be matched by this pattern because it's at least one uppercase character and nothing else also something like this would be matched and if you want to play around with this if you're using pycharm you can just go into this string here and you can press alt enter and you can go to check wreck at rec x and uh you can check if the regular expression matches the sample so if i write something here you can see it doesn't match the sample if i have an empty string it says also it doesn't match the sample but if i now type uppercase characters you can see that it matches uh the sample now if we remove here the anchor uh tag and we also remove the dollar and i check the regex again i can also provide some nonsense and i can provide some uppercase characters and it's still going to be matched because it's not the beginning of the string and not the end of the string but we do have a chain of uppercase characters here so the anchor tag in the beginning and the dollar sign in the end are important to say the whole string can only consist of whatever is in between here so we can go ahead now and say okay uh give me a string so we're gonna print here the pattern dot search and we're gonna search for uh this pattern we're gonna start with a simple hello world this is not gonna work then we're gonna say um hello world in capital letters but still with a space and then we're gonna say hello world but without the space here in uppercase and you can see here we have none none and then we have a match down here being hello world now if i add something in the end here it's not going to work because not the whole string is only consisting of uppercase characters and you can see already how this works you define a pattern for example it has to be an uppercase word and nothing else and then you can just get any string and see if this string matches the pattern so you can see if the string conforms with the rules that we define here in this case they're quite arbitrary not really important not really interesting rules we can also do the same thing with lowercase characters so if i change the a to a lowercase a and the z to a lowercase z now only lowercase characters will be accepted which is why we get none not none um and if i change this now here to hello world and if i change this here now to hello world without a space you can see that we get the same result here only the last one found a match so only in the last string did we find a match for this pattern because this is the only string that only consists of lowercase characters so i think if we remove the dollar and the anchor tag we should find also something in the second string which is hello and here we can also find yellow now the difference between search and match by the way for those of you who saw that there is a match function is that the match function as far as i remember looks into the whole string whereas the search function looks only into the beginning if i'm not mistaken i hope i'm not seeing anything wrong here um but uh no sorry the other way around the search function looks in the whole string so for example the search function finds hello here even though the first character does not match this pattern whereas the match function will only find it if it starts with that so if it already starts with a lowercase character so you can see the match here does not find the yellow which is lowercase which fits that criteria because the first character is an uppercase character so it's already not finding a pattern whereas the search function here the search method does find the yellow as you can see because there is the pattern um even though it doesn't start uh even though the string doesn't start with that pattern however if we add an anchor tag as we did before and a dollar the whole string has to match the pattern all right so that's that now let's move on to some more of fancy patterns so first of all let me show you that we can also allow for mixed case so we can say a to z we can also say capital a to capital z in the same bracket so like that this says that it has to be a lowercase or an uppercase character uh and we can also add in here uh just append here also a backslash s allowing also for spaces and now we're going to find all of those because we have a space we have a to z capital a to z lowercase but if i add for example a number here like eight it's not going to find a pattern here because that is a digit and a digit is not specified above we can only have lowercase uppercase characters and a space which is uh the backslash s character here all right so let's do some arbitrary pattern let's say we have i don't know some game and we have certain keys that we want to check for and we want to have the pattern we're going to write this here as a comment now so we don't lose the orientation here we want to have three lowercase letters followed by three to five digits followed by one symbol a symbol being defined at any special character that is not a letter or not a digit and i don't know space maybe um and then we're gonna say also we wanna have afterwards two uppercase characters or we can say up to two uppercase characters because those are going to be optional so we can see what we can do here so for example what would be a possible code here we could say hzu and then 6682 and then hashtag and then k that could be a code that is allowed here we're going to check for that now we're going to define a pattern that fits those criteria so how do we do that uh this is going to be a crash course briefly on regular expressions only the basics are going to be taught here we're going to say that the string has to start first of all with the pattern so we want to check that all of the string conforms to the pattern so we're going to have uh an anchor tag and want to have a dollar here in the end everything is going to be in between so what we do first is we say we want to have lowercase characters how do we do that a to z we already learned that now instead of saying plus because plus means at least once and beyond that how many times you want we're gonna provide now the square uh the the curly brackets here saying that we wanna have exactly three lowercase letters then we're going to say 0 2 9 those are the digits when i have from those 3 to 5 and this is defined by saying 3 comma 5 in curly brackets so this is the range 3 4 and 5 is allowed um then we want to have one symbol how do we define a symbol now this is a little bit tricky we're going to use the following definition here we want to say everything that is not an uppercase character is not a lowercase character and it's also not a digit will be considered uh to be a special character so we're going to say a to z a to z 0 to 9 in the beginning of those square brackets so right after the square bracket we're going to add another anchor tag like this this is going to say not all of this so not all of this everything that's not part of this definition here once and then we're going to say optionally we can have two uppercase characters how do we do that uh quite simple a to z and we're gonna say the range is zero to two so zero one and two are fine this is the pattern we can go ahead and play around with it so if i say for example um abc one two three um hashtag and then some uppercase characters like that this matches the pattern and we can play around with that now so i can say for example hd uh two three three one then hashtag aj then maybe something else like ll uh four four five one one and then i don't know point or dot k and we can maybe copy this one here and remove the k because that's still valid and now let's also add one that's not valid so for example um abc this is still valid and then i'm gonna say six digits instead of uh something between three and five so i can say one two three four five six and then we can say for example hashtag and the rest is going to be fine like uh jj there you go and you can see those are all matched this one is not matched so if we have a rule like that we can write it as a regex we don't have to do any if else statements we just have this pattern and then this pattern is applied onto the string and we look for it we search for it if we find it we validate the string we say okay now we have a match so we can move on um and yeah we can also look at one more quite simple pattern before we move to the final pattern which can be quite useful here um and one very simple pattern for example is to just say i want to have any character where i can have any string as long as it has a length of 10. so i can say it doesn't matter what the string is digits special characters uh letters whatever it has to have a length of 10 that's the only criteria how do we do that we say anchor tag we say dollar we say dot the dot representing basically everything so a dot unless you escape it like that because that's then the actual dot but if you just say dot this means any character whatsoever and we can say okay i want to have whatever character 10 times this is a string and now i can just say print pattern dot search and i can say a b c d e f g h i think h is no where's ten one two three four five six seven eight i j there you go and then we're gonna do zero one two three four five six seven eight nine uh we can also do whatever we want we can change this to a dot we can change this to a hashtag we can change this to a j we can change this to a j as well we can change this here to a space it doesn't matter as long as as it has the length uh 10 if i go now again with a b c d e f g h i j and i add a k or if i remove a j in the end those are not going to be matched because they don't have a length of 10. so quite simple i think you get it you get the basic idea of a regex you define the rules you apply them you search for them now last but not least let's look at validating emails because that can be a quite a reasonable task a quite useful task and i'm not saying we should do it the way i show it here today because i'm going to use a very simple regular expression because if you want to have a proper regular expression that really takes care of all the cases uh you can not expect this to be as simple as what i'm going to show you here even though probably for those of you who are new to regular expressions this is going to be quite complicated already um you cannot expect any regular expression that's that simple to validate emails properly with all the possible edge cases so maybe you want to use a predefined library for that however we're going to say now pattern equals re compound we're going to say a simplified version of the email in our understanding is that we want to have any name like your email name whatever it is and this is going to be basically lowercase characters uppercase characters digits are also going to be allowed and then a couple of special characters for example a dot for example um a dash and for example an underscore now the dot needs to be escaped and the dash needs to be escaped as well uh with dot i'm not actually sure if it has to be escaped in here um but we're going to escape it because otherwise maybe it's going to be interpreted as any character the underscore does not have to be escaped so basically that however many times we want but at least once so basically a plus followed by an add symbol so just the add symbol here which is going to occur exactly once and then we're going to say okay again lowercase characters uppercase characters and digits for the domain name so for example mail at neural9.com would be an email this here would be the mail this year would be the add this here would be the neural nine uh then this is going to be followed by uh so first of all this has to be plus again then this is going to be followed by a dot now let me just make sure i'm not blocking this for you guys here um this is going to be a dot and then we're going to say this dot has to occur exactly once and then it's going to be followed by some ending like dot com or dot net or dot d e dot a t dot uk or something like that so we're going to say a z a z uppercase lowercase doesn't really matter and this is going to be two or three now this is a very limited domain so for example or not domain this is a very limited pattern because let me show you that it works essentially so i can say print pattern dot search and i'm going to say now i have mail at neural9.com for example this is going to work if i say something else like mymail.test.com this is going to work um if i say my underscore fancy dash e or dot e dash mail at fancy url one two three dot d e this is also gonna work because that's a valid email uh if i say something dot something dot com or something without an ad symbol here for example or if i just say something like mail adds uh mail at something without a dot in the end this is also not gonna work so this you can see right away it finds these it validates these emails and it does not validate these emails but the problem is of course that a url like this one is fine but if i have something like for example dot game i think we have some fancy um domain names or domain endings like that nowadays dot game or dot i don't know if there's something like dot travel or something like that those are not gonna be uh possible those are not gonna be recognized because of course we're limited to two to three now of course you can also change this up to nine or something but the problem is then okay you allow again for a lot of stuff so maybe you want to have a list maybe you want to use a different approach but this is one very simplistic way to check for emails you can just define a simple regular expression like that let me repeat what it does it takes basically any character that is a ladder a digit uh a dot a hyphen or an underscore we need at least one of those but we can have as many as we want then followed by one at symbol follow followed by any number of at least one but any number beyond that of letters or digits by the way we need to change this to a capital z um then followed by a dot and then followed again by something between the length of two and three initially here that is only uh consisting of characters and this has to be the whole string so yeah this is how you use regular expressions in python so that's it for today's video i hope you enjoyed it and hope you learned something if so let me know by hitting a like button leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you

Original Description

In this video, we learn how to use regular expressions in Python to validate or check user input. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to use regular expressions in Python to validate or check user input, providing a practical introduction to string pattern matching.

Key Takeaways
  1. Import the re module in Python
  2. Use the search function to find patterns in strings
  3. Use the match function to match patterns at the beginning of strings
  4. Use the findall function to find all occurrences of a pattern in a string
  5. Use regular expressions to validate user input
💡 Regular expressions can be used to effectively validate and check user input in Python, improving the robustness and reliability of programs.

Related Reads

📰
Cursor Pricing 2026: Free vs Pro vs Ultra — Which Plan?
Learn how to choose the right Cursor plan for your coding needs and budget, and discover how this Agentic AI coding tool can boost your productivity
Dev.to AI
📰
enable Consistent AI Coding with Persistent Context Layers
Learn how persistent context layers can improve AI coding consistency and reliability
Dev.to AI
📰
LeetCode Isn’t Dead. Your Interview Prep Strategy Is.
Update your interview prep strategy to focus on practical skills and real-world problem-solving, as LeetCode-style interviews are evolving
Medium · Programming
📰
Build a UGC video moderation pipeline with FFmpeg + NudeNet
Learn to build a UGC video moderation pipeline using FFmpeg and NudeNet to ensure safe and respectful user-generated content
Dev.to · Mason K
Up next
Copilot Cowork: Setup, Skills, Plugins & Pricing
Matt Tutorials
Watch →