Regular Expression Methods in Python

Alex The Analyst · Beginner ·📊 Data Analytics & Business Intelligence ·2y ago

Key Takeaways

The video covers Regular Expression Methods in Python, including various methods for working with regular expressions in Python.

Full Transcript

what's going on everybody welcome back to another video today we're going to be starting our regular expression Series in Python now regular expression is used to identify and specify a pattern of characters that you're looking for in your text it sounds pretty straightforward but the actual syntax can be pretty confusing at first but throughout this series I'll try to make it as simple as I possibly can by the end of it you should be using regular expression like a pro so without further Ado let's jump onto my screen and get started alright so the first thing that we're going to take a look at is the re module plus regex methods the re module is just the module that you need to import in order to use regular expression in Python we'll try out each of these and keep it really simple and then in the next several lessons we'll look at how you can do more complex patterns to actually search so really quickly before we get started let's just take a look at these methods we have find all which is going to return a list of all your matches we have search which actually creates a match object so we won't actually see what we're actually returning and we'll look at that in just a little bit but there's a way to act access that and it's fairly simple we also have split which is going to return a list where the string has been split on that specific pattern that you want the last one is sub so it's just a replacement you're going to replace a specific string with another string so let's come right down here and the first thing that we're going to do is we're going to import the regular expression module so all we have to do is run this and we're going to come right down here now what we're going to do is just test out all these different methods and we'll do this just on a simple string for now so we'll do quote and for this variable we'll create a quote here and let's actually use double quotes for this one and what we're going to say is we're going to say there's only one thing I hate more than lying skim milk which is water that's lying about being milk and that is from Ron Swanson so let's go ahead and run this and we have our quote now so we're going to be using this and the first one that we're going to use is actually the search this is the one that I don't use as much I want to kind of get it out of the way before I use find all split and sub so search is going to create that match object does come right here we can see both and what we're going to do is going to say re so we're calling the regular expression module we're going to read.search and we're going to search for a specific pattern now we're going to keep it really simple like I said we're just going to do milk we're searching for milk and where are we searching we're searching within the quote and that's all we need let's go ahead and run this and like I said before it's returning this match object so it's been created and it even tells us that there was a match at the 52 to 56 position and that the match was milk now notice that it only got one of the matches so we actually have milk twice in here we have milk over here and way over here now if we want to use this we want to see what it was we can also do group and if we run this we can see that there is milk and that's what we're searching for and that's what was put into that match object I myself don't use search a lot although if you're working with like a huge book and you're searching for a specific pattern um this actually can be more efficient than some of these other methods that we're going to be taking a look at but I just typically don't use this one as much but that is how it works now let's look at find all let's come right down here let's say re and we're going to do dot find all and again we're going to search for milk but this time when we're looking at it we're doing find all which is going to find all of the results for this pattern let's go ahead and run this now notice that this one made a list and this is extremely helpful if you want to take all these values that match a specific pattern and put it into a list this final is extremely helpful that's why I personally probably like this one the most if you can have a favorite uh regex method this one is probably my favorite because it puts it into a list for you it's formatted and remember right now we're doing a super super simple just searching for a really simple string in future lessons when we look at really more advanced patterns we'll be able to take a lot of different things and put them into this list not just you know one word like milk something that you can use this for is finding out how many are actually in here so you can use this with the length function or the length function if we run this we can see that we have two milk in this quote that's at least something that I use it for I think is pretty helpful when I'm looking to just see how many are actually in a quote or a text or you know whatever I'm working with now let's take a look at split let's come right down here we're going to say re dot split and this one is going to split on the pattern that you specify so if we do the exact same thing like we've been doing which it makes absolutely no sense but we'll do it really quickly um we're going to run this if we do it on milk notice that milk comes says there's only one thing I hate more than lying skim milk so skim is where we're going to stop that first string right here then we have a comma and notice this is all in a list so we have our first value then we have a comma and then we go to our next one which is lying about being which is right here which is lying about being and then we have milk that's where it cuts off so we're splitting it there as well then we have Ron Swanson so it's splitting it up based off of the specified value now milk makes absolutely no sense what we would want to do is split it up on something like a period now this is a very real use case where I've taken something like a webinar or taking a speech and I've separated it by its actual sentences which is something like this now if we do a period this actually represents a meta character that's going to be something that we take a look at in the next lesson if we run this it stands for everything so it doesn't really work what we need to do is use a backslash period actually specify that we're looking at periods like this the period actually stands for any character so in the next lesson we'll dive much more into that but let's split this based off of the period the actual period so we're going to say there's only one thing I hate more than lying skim milk that's the first sentence then we have which is water that's lying about being milk and then we have Ron Swanson so we split it up based off of the period and it makes a much more logical sense when we're actually splitting into values and putting it into our list the last one that we're going to take a look at is sub and this one is pretty straightforward you look for a specific pattern and you replace it with a different pattern so let's go ahead and try this we'll do re dot sub and then we're going to specify the pattern that we're searching for so I'll say I and then what do we want to replace it with and we'll just do U we'll keep it super simple again we'll look at our quote and let me actually bring this down so we can see it right here so I'm saying re dot sub I'm replacing the I which I'm I believe it's just here and I'm replacing it with you and let's go ahead and run this and now it says there's only one thing you hate more than lying skim milk but we could do anything and we can do multiple values we can say milk as well let's go ahead and replace milk with uh skim Dairy I don't know I think that makes sense um so now we have dairy and dairy so we're able to replace all of them there is one more parameter in this function that we can use we can come right here and we can specify the count so I'll just explicitly say count but you could just do the comma or I'll just say one now if I specify one it's only going to replace the first one let's go ahead and run this and you'll notice that this one is dairy and this one is milk and those are all the regex methods that we're going to look at in this lesson now like I said before these are very simple patterns that we're looking for this is not how regex is going to be used when you've learned everything this is just how we're using it to show these regex methods in the next two lessons we're gonna be looking at meta characters and character classes and those are really going to allow you to specify what pattern you're looking for not just a hard value like milk but what pattern you're actually wanting to search for so thank you guys so much for watching I will see you in the next video foreign [Music]

Original Description

Take my Full Python Course Here: https://bit.ly/48O581R In this lesson we are going to look at Methods for Regular Expression in Python! GitHub Code: https://bit.ly/3UURfrW ____________________________________________ SUBSCRIBE! Do you want to become a Data Analyst? That's what this channel is all about! My goal is to help you learn everything you need in order to start your career or even switch your career into Data Analytics. Be sure to subscribe to not miss out on any content! ____________________________________________ RESOURCES: Coursera Courses: 📖Google Data Analyst Certification: https://coursera.pxf.io/5bBd62 📖Data Analysis with Python - https://coursera.pxf.io/BXY3Wy 📖IBM Data Analysis Specialization - https://coursera.pxf.io/AoYOdR 📖Tableau Data Visualization - https://coursera.pxf.io/MXYqaN Udemy Courses: 📖Python for Data Science - https://bit.ly/3Z4A5K6 📖Statistics for Data Science - https://bit.ly/37jqDbq 📖SQL for Data Analysts (SSMS) - https://bit.ly/3fkqEij 📖Tableau A-Z - http://bit.ly/385lYvN *Please note I may earn a small commission for any purchase through these links - Thanks for supporting the channel!* ____________________________________________ BECOME A MEMBER - Want to support the channel? Consider becoming a member! I do Monthly Livestreams and you get some awesome Emoji's to use in chat and comments! https://www.youtube.com/channel/UC7cs8q-gJRlGwj4A8OmCmXg/join ____________________________________________ Websites: 💻Website: AlexTheAnalyst.com 💾GitHub: https://github.com/AlexTheAnalyst 📱Instagram: @Alex_The_Analyst ____________________________________________ *All opinions or statements in this video are my own and do not reflect the opinion of the company I work for or have ever worked for*
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Alex The Analyst · Alex The Analyst · 0 of 60

← Previous Next →
1 Top 3 Data Analyst Skills in 2020
Top 3 Data Analyst Skills in 2020
Alex The Analyst
2 Truth About Big Companies | Told by a Fortune 500 Data Analyst
Truth About Big Companies | Told by a Fortune 500 Data Analyst
Alex The Analyst
3 Data Analyst Salary | 100k with No Experience
Data Analyst Salary | 100k with No Experience
Alex The Analyst
4 Working at a Big Company Vs Small Company | Told by a Fortune 500 Data Analyst
Working at a Big Company Vs Small Company | Told by a Fortune 500 Data Analyst
Alex The Analyst
5 Data Analyst Resume | Reviewing My Resume! | Fortune 500 Data Analyst
Data Analyst Resume | Reviewing My Resume! | Fortune 500 Data Analyst
Alex The Analyst
6 Data Analyst Resume | Complete Guide To Creating A Data Analyst Resume | Tips + Templates + Examples
Data Analyst Resume | Complete Guide To Creating A Data Analyst Resume | Tips + Templates + Examples
Alex The Analyst
7 Switching Careers to Become a Data Analyst | How I Made the Switch
Switching Careers to Become a Data Analyst | How I Made the Switch
Alex The Analyst
8 Working With a Recruiter to Land Your First Job as a Data Analyst | LinkedIn Recruiters
Working With a Recruiter to Land Your First Job as a Data Analyst | LinkedIn Recruiters
Alex The Analyst
9 Data Analyst Salary in 2020
Data Analyst Salary in 2020
Alex The Analyst
10 Data Analyst Resume | Reviewing YOUR Data Analyst Resumes!
Data Analyst Resume | Reviewing YOUR Data Analyst Resumes!
Alex The Analyst
11 Data Analyst Fact Check |  84k Average Starting Salary?? | The Career Force 2020 Data Analyst Salary
Data Analyst Fact Check | 84k Average Starting Salary?? | The Career Force 2020 Data Analyst Salary
Alex The Analyst
12 SQL Basics Tutorial For Beginners | Installing SQL Server Management Studio and Create Tables | 1/4
SQL Basics Tutorial For Beginners | Installing SQL Server Management Studio and Create Tables | 1/4
Alex The Analyst
13 SQL Basics Tutorial For Beginners | Select + From Statements | 2/4
SQL Basics Tutorial For Beginners | Select + From Statements | 2/4
Alex The Analyst
14 SQL Basics Tutorial For Beginners | Where Statement | 3/4
SQL Basics Tutorial For Beginners | Where Statement | 3/4
Alex The Analyst
15 SQL Basics Tutorial For Beginners | Group By + Order By Statements | 4/4
SQL Basics Tutorial For Beginners | Group By + Order By Statements | 4/4
Alex The Analyst
16 Day in the Life of a Data Analyst | Fortune 500 Edition
Day in the Life of a Data Analyst | Fortune 500 Edition
Alex The Analyst
17 Intermediate SQL Tutorial | Inner/Outer Joins | Use Cases
Intermediate SQL Tutorial | Inner/Outer Joins | Use Cases
Alex The Analyst
18 Intermediate SQL Tutorial | Unions | Union Operator
Intermediate SQL Tutorial | Unions | Union Operator
Alex The Analyst
19 Intermediate SQL Tutorial | Case Statement | Use Cases
Intermediate SQL Tutorial | Case Statement | Use Cases
Alex The Analyst
20 Intermediate SQL Tutorial | Having Clause
Intermediate SQL Tutorial | Having Clause
Alex The Analyst
21 Intermediate SQL Tutorial | Updating/Deleting Data
Intermediate SQL Tutorial | Updating/Deleting Data
Alex The Analyst
22 Day in the Life of a Data Analyst | Fortune 500 Edition (During Quarantine)
Day in the Life of a Data Analyst | Fortune 500 Edition (During Quarantine)
Alex The Analyst
23 Data Analyst Interview Questions | Phone + In-Person Interview Questions
Data Analyst Interview Questions | Phone + In-Person Interview Questions
Alex The Analyst
24 SQL Interview Questions and Answers for Beginners | Data Analyst Interview Questions
SQL Interview Questions and Answers for Beginners | Data Analyst Interview Questions
Alex The Analyst
25 Data Analyst Interview Questions | What To Say vs What NOT To Say
Data Analyst Interview Questions | What To Say vs What NOT To Say
Alex The Analyst
26 Data Analyst Interviews | Salary Negotiation
Data Analyst Interviews | Salary Negotiation
Alex The Analyst
27 Data Analyst Q&A LIVE
Data Analyst Q&A LIVE
Alex The Analyst
28 Intermediate SQL Tutorial | Aliasing
Intermediate SQL Tutorial | Aliasing
Alex The Analyst
29 Data Scientist vs Data Analyst | Which Is Right For You?
Data Scientist vs Data Analyst | Which Is Right For You?
Alex The Analyst
30 Best Online Courses for Data Analysts
Best Online Courses for Data Analysts
Alex The Analyst
31 Best Free Online Courses for Data Analysts
Best Free Online Courses for Data Analysts
Alex The Analyst
32 Data Analyst vs Business Analyst | Which Is Right For You?
Data Analyst vs Business Analyst | Which Is Right For You?
Alex The Analyst
33 Scraping Data Off Twitter Using Python | Twitterscraper + NLP + Data Visualization
Scraping Data Off Twitter Using Python | Twitterscraper + NLP + Data Visualization
Alex The Analyst
34 Data Analyst Question and Answer | Answering Your YouTube Questions
Data Analyst Question and Answer | Answering Your YouTube Questions
Alex The Analyst
35 What Does a Data Analyst Actually Do?
What Does a Data Analyst Actually Do?
Alex The Analyst
36 Data Analyst Bootcamps | Are They Worth It?
Data Analyst Bootcamps | Are They Worth It?
Alex The Analyst
37 Top 5 Reasons Not to Become a Data Analyst
Top 5 Reasons Not to Become a Data Analyst
Alex The Analyst
38 Data Analyst Career Path | How to Become a Data Analyst + What to Do Next
Data Analyst Career Path | How to Become a Data Analyst + What to Do Next
Alex The Analyst
39 Live Data Analyst Q&A #3
Live Data Analyst Q&A #3
Alex The Analyst
40 Top 5 Reasons Not to Lie on Your Resume
Top 5 Reasons Not to Lie on Your Resume
Alex The Analyst
41 The Hiring Process from an Interviewer's Perspective | Alex The Analyst Show | Episode 1
The Hiring Process from an Interviewer's Perspective | Alex The Analyst Show | Episode 1
Alex The Analyst
42 Top 5 Reasons Data Analytics is a Good Career Choice
Top 5 Reasons Data Analytics is a Good Career Choice
Alex The Analyst
43 How I Changed Careers to Become a Data Analyst | Alex The Analyst Show | Episode 2
How I Changed Careers to Become a Data Analyst | Alex The Analyst Show | Episode 2
Alex The Analyst
44 Top 5 Reasons You'll Be a Good Data Analyst
Top 5 Reasons You'll Be a Good Data Analyst
Alex The Analyst
45 Self Taught vs Boot Camp vs Degree | Alex The Analyst Show | Episode 3
Self Taught vs Boot Camp vs Degree | Alex The Analyst Show | Episode 3
Alex The Analyst
46 Covid and the Data Analyst Job Market | Alex The Analyst Show | Episode 4
Covid and the Data Analyst Job Market | Alex The Analyst Show | Episode 4
Alex The Analyst
47 Data Analyst Expectations vs Reality
Data Analyst Expectations vs Reality
Alex The Analyst
48 Imposter Syndrome in Tech | Alex The Analyst Show | Episode 5
Imposter Syndrome in Tech | Alex The Analyst Show | Episode 5
Alex The Analyst
49 Top 10 Coursera Courses for Data Analysts
Top 10 Coursera Courses for Data Analysts
Alex The Analyst
50 Working at a Startup vs Fortune 500 Company | Alex The Analyst Show | Episode 6
Working at a Startup vs Fortune 500 Company | Alex The Analyst Show | Episode 6
Alex The Analyst
51 Data Analyst Certifications | Are They Worth It? | Alex The Analyst Show | Episode 7
Data Analyst Certifications | Are They Worth It? | Alex The Analyst Show | Episode 7
Alex The Analyst
52 Top 10 Udemy Courses for Data Analysts
Top 10 Udemy Courses for Data Analysts
Alex The Analyst
53 Asking My Wife Your Questions About Me | Alex The Analyst Show | Episode 8
Asking My Wife Your Questions About Me | Alex The Analyst Show | Episode 8
Alex The Analyst
54 Data Analyst Q&A LIVE #4
Data Analyst Q&A LIVE #4
Alex The Analyst
55 Data Analyst Skills Path | What Skills You NEED to Know
Data Analyst Skills Path | What Skills You NEED to Know
Alex The Analyst
56 What is Analytics Consulting? With John Ariansen | Alex The Analyst Show | Episode 9
What is Analytics Consulting? With John Ariansen | Alex The Analyst Show | Episode 9
Alex The Analyst
57 Solving LeetCode SQL Interview Questions | Part 1/3
Solving LeetCode SQL Interview Questions | Part 1/3
Alex The Analyst
58 What is No Code Analytics? | Alex The Analyst Show | Episode 10
What is No Code Analytics? | Alex The Analyst Show | Episode 10
Alex The Analyst
59 Top 3 Tips on Using LinkedIn to Land a Job
Top 3 Tips on Using LinkedIn to Land a Job
Alex The Analyst
60 Completely Unrealistic Jobs on LinkedIn | Alex The Analyst Show | Episode 11
Completely Unrealistic Jobs on LinkedIn | Alex The Analyst Show | Episode 11
Alex The Analyst

This video teaches viewers how to work with regular expressions in Python, covering various methods and techniques for data analysis. Viewers will learn how to use Python for data science and improve their data literacy skills.

Key Takeaways
  1. Import the re module in Python
  2. Use the search() method to find patterns in strings
  3. Use the match() method to match patterns at the beginning of strings
  4. Use the findall() method to find all occurrences of a pattern in a string
  5. Use the sub() method to replace occurrences of a pattern in a string
💡 Regular expressions are a powerful tool for working with text data in Python, and can be used for a variety of tasks such as data cleaning and data extraction.

Related Reads

📰
One Decision Can Change Your Career: Why Thousands of Students Are Choosing Browsejobs to Learn…
Learn how one decision can impact your career with Browsejobs, a platform helping thousands of students learn and grow in data science
Medium · Data Science
📰
Why Do US Stock Minute Bar Backtests Fail to Match Live Trading Results?
Learn why US stock minute bar backtests often fail to match live trading results and how to improve their accuracy
Dev.to · James Tao
📰
Simulating Trade Outcomes with Parkinson Volatility
Learn to simulate trade outcomes using Parkinson volatility with a Python Monte Carlo example
Medium · Data Science
📰
Automating 1099-NEC Generation for Freelance Bookkeepers: The Data Hygiene Framework
Learn to automate 1099-NEC generation for freelance bookkeepers using a data hygiene framework to overcome data quality issues
Dev.to AI
Up next
SQL Interview Question on Retention. #sql #dataanalytics #datascience
Rajeev Kanth | BEPEC
Watch →