Regular Expression in Python

Regular Expression in Python
We will start with Regular Expressions. RegEx is a way of creating a rule to filter what you want from your data. We will keep Python as a consistent coding language for this complete exercise. Let’s start with Regular Expression in Python

Many of you must have come across SQL questions where you need to get the data of customers whose name starts with A and in the WHERE condition you write something like,

WHERE Customer_Name LIKE ‘A%’

Well !! This is the basic Regular Expression where you request the query to get you a specific result. The way we write Regular Expression in Python is a bit different. Check out the table below:-To use Regular Expression, first, you need to “import re” package
And the following 4 functions are quite useful for using your regex

1. findall – It returns a complete list of all the matches
2. search – It returns a match object
3. split – Splits the string wherever there is a match
4. sub – It replaces one or many matches of the regex

Following are some important metacharacter and special sequence

RegExDescription
w+Get all the words
dDigits
sSpaces
SAnything but white spaces
+One or more occurrences
^Starts with
$Ends with
*Zero or more occurrences
+One or more occurrences
|Either Or
[]A set of Character

1. Get the string that starts with The and ends with Monk

import re

st = “The Data Monk”
x = re.search(“^The.*Monk$”, st)

2. Get the start and end index of a string ‘Learning’

import re

s = ‘Experience the Learning curve’

match = re.search(r’Learning’, s)

print(‘Start Index:’, match.start())
print(‘End Index:’, match.end())

The above code gives the starting index and the ending index of the string portal.

3. Finding all occurrences of a pattern 

string = “””You can contact us at 979804930 or 94310038″””

regex = ‘\d+’

match = re.findall(regex, string)
print(match)

The above will print the two numbers

The Data Monk Interview Books – Don’t Miss

Now we are also available on our website where you can directly download the PDF of the topic you are interested in. At Amazon, each book costs ~299, on our website we have put it at a 60-80% discount. There are ~4000 solved interview questions prepared for you.

10 e-book bundle with 1400 interview questions spread across SQL, Python, Statistics, Case Studies, and Machine Learning Algorithms – Ideal for 0-3 years experienced candidates

23 E-book with ~2000 interview questions spread across AWS, SQL, Python, 10+ ML algorithms, MS Excel, and Case Studies – Complete Package for someone between 0 to 8 years of experience (The above 10 e-book bundle has a completely different set of e-books)

12 E-books for 12 Machine Learning algorithms with 1000+ interview questions – For those candidates who want to include any Machine Learning Algorithm in their resume and to learn/revise the important concepts. These 12 e-books are a part of the 23 e-book package

Individual 50+ e-books on separate topics

Important Resources to crack interviews (Mostly Free)

There are a few things which might be very useful for your preparation

The Data Monk Youtube channel – Here you will get only those videos that are asked in interviews for Data Analysts, Data Scientists, Machine Learning Engineers, Business Intelligence Engineers, Analytics managers, etc.
Go through the watchlist which makes you uncomfortable:-

All the list of 200 videos
Complete Python Playlist for Data Science
Company-wise Data Science Interview Questions – Must Watch
All important Machine Learning Algorithm with code in Python
Complete Python Numpy Playlist
Complete Python Pandas Playlist
SQL Complete Playlist
Case Study and Guesstimates Complete Playlist
Complete Playlist of Statistics