Regular Expression Tutorial – Day 14
Topic – Regular Expression Tutorial
Define Regular Expression in layman’s term
In simple terms, a regular expression, often referred to as a “regex,” is a sequence of characters that forms a search pattern. It is used for matching strings or parts of strings, helping you find specific patterns within a larger piece of text.
To give you an example, consider a situation where you’re searching for phone numbers within a long document. Instead of reading every line to find each number, you can use a regular expression that represents the pattern of a typical phone number. This can help you quickly identify and extract all the phone numbers in the text, saving you time and effort.
In summary, regular expressions serve as powerful tools to help you search, manipulate, and manage text by identifying specific patterns or sequences of characters. They are widely used in various programming languages and text editing applications to facilitate tasks like data validation, searching, and replacing text, and data extraction.
Regular Expression Tutorial
20 Regular Expression Questions
1.What is a regular expression?
Answer: A regular expression, often abbreviated as regex, is a sequence of characters that defines a search pattern.
2. What are the benefits of using regular expressions?
Answer: Regular expressions allow for powerful and flexible pattern matching, making it easier to perform complex search and replace operations in text.
3.How do you create a basic regular expression in Python?
Answer: Use the re module in Python to create a basic regular expression.
Example code:
import re
pattern = r”hello”
text = “hello world”
result = re.search(pattern, text)
4. What is the purpose of the ‘re.search()’ function in Python?
Answer: The re.search() function is used to search for a match to a pattern within a string.
5.How do you match the beginning of a string in a regular expression?
Answer: Use the ^ symbol to match the beginning of a string.
Example code:
pattern = r”^hello”
text = “hello world”
result = re.search(pattern, text)
6.How do you match the end of a string in a regular expression?
Answer: Use the $ symbol to match the end of a string.
Example code:
pattern = r”world$”
text = “hello world”
result = re.search(pattern, text)
7.How do you match any single character in a regular expression?
Answer: Use the . symbol to match any single character.
8.How do you match any single digit in a regular expression?
Answer: Use the \d pattern to match any single digit.
9.How do you match any single word character in a regular expression?
Answer: Use the \w pattern to match any single word character.
10.How do you match any single whitespace character in a regular expression?
Answer: Use the \s pattern to match any single whitespace character.
11.How do you match a specific set of characters in a regular expression?
Answer: Use square brackets [] to create a character set.
Example code:
pattern = r”[aeiou]”
text = “hello world”
result = re.findall(pattern, text)
12.How do you match a specific range of characters in a regular expression?
Answer: Use a hyphen – to specify a range of characters within square brackets.
13.How do you match a specific set of characters or digits in a regular expression?
Answer: Use the | symbol to match any of the specified patterns.
Example code:
pattern = r”cat|dog”
text = “I have a cat and a dog”
result = re.findall(pattern, text)
14.How do you match a specific number of occurrences of a character in a regular expression?
Answer: Use the {} curly brackets to specify the number of occurrences.
15.How do you match zero or more occurrences of a character in a regular expression?
Answer: Use the * symbol to match zero or more occurrences.
16.How do you match one or more occurrences of a character in a regular expression?
Answer: Use the + symbol to match one or more occurrences.
17.How do you match zero or one occurrence of a character in a regular expression?
Answer: Use the ? symbol to match zero or one occurrence.
18.How do you match a specific word in a regular expression?
Answer: Use the word itself to match the word in the text.
Example code:
pattern = r”hello”
text = “hello world”
result = re.search(pattern, text)
19.How do you match the opposite of a specified pattern in a regular expression?
Answer: Use the ^ symbol inside square brackets to match the opposite of a pattern.
20.How do you match the occurrence of a character at the beginning of a word in a regular expression?
Answer: Use the \b pattern to match the beginning of a word.
Complex Regular Expression Questions
Here are 10 complex regular expression questions in Python along with their answers:
Question 1: Check if a string contains only letters (both lowercase and uppercase).
import re
pattern = “^[a-zA-Z]+$”
string = “AbCdEfG”
result = re.match(pattern, string)
print(bool(result))
Question 2: Check if a string contains at least one digit.
import re
pattern = “.\d+.“
string = “abc123”
result = re.match(pattern, string)
print(bool(result))
Question 3: Extract all email addresses from a string.
import re
pattern = r'[\w.-]+@[\w.-]+’
string = “Contact us at [email protected] or [email protected]”
result = re.findall(pattern, string)
print(result)
Question 4: Replace all occurrences of ‘is’ with ‘was’ in a string.
import re
pattern = r’\bis\b’
string = “He is happy, she is sad, it is raining.”
result = re.sub(pattern, ‘was’, string)
print(result)
Question 5: Check if a string starts with a capital letter.
import re
pattern = “^[A-Z].*”
string = “Hello”
result = re.match(pattern, string)
print(bool(result))
Question 6: Split a string into words.
import re
string = “This is a sample string.”
result = re.split(r’\s’, string)
print(result)
Question 7: Validate a phone number (XXX-XXX-XXXX) format.
import re
pattern = r’\d{3}-\d{3}-\d{4}’
string = “123-456-7890”
result = re.match(pattern, string)
print(bool(result))
Question 8: Extract all words that start with ‘a’ or ‘b’.
import re
pattern = r’\b[a-b]\w*’
string = “An apple a day keeps the doctor away, but a banana works too.”
result = re.findall(pattern, string)
print(result)
Question 9: Check if a string has at least 5 characters.
import re
pattern = r’^.{5,}$’
string = “Hello”
result = re.match(pattern, string)
print(bool(result))
Question 10: Remove all punctuation from a string.
import re
string = “Hello, World! This is a sample string.”
result = re.sub(r'[^\w\s]’, ”, string)
print(result)
Our services
- YouTube channel covering all the interview-related important topics in SQL, Python, MS Excel, Machine Learning Algorithm, Statistics, and Direct Interview Questions
Link – The Data Monk Youtube Channel - Website – ~2000 completed solved Interview questions in SQL, Python, ML, and Case Study
Link – The Data Monk website - E-book shop – We have 70+ e-books available on our website and 3 bundles covering 2000+ solved interview questions
Link – The Data E-shop Page - Instagram Page – It covers only Most asked Questions and concepts (100+ posts)
Link – The Data Monk Instagram page - Mock Interviews
Book a slot on Top Mate - Career Guidance/Mentorship
Book a slot on Top Mate - Resume-making and review
Book a slot on Top Mate
The Data Monk e-books
We know that each domain requires a different type of preparation, so we have divided our books in the same way:
✅ Data Analyst and Product Analyst -> 1100+ Most Asked Interview Questions
✅ Business Analyst -> 1250+ Most Asked Interview Questions
✅ Data Scientist and Machine Learning Engineer -> 23 e-books covering all the ML Algorithms Interview Questions
✅ Full Stack Analytics Professional – 2200 Most Asked Interview Questions
The Data Monk – 30 Days Mentorship program
We are a group of 30+ people with ~8 years of Analytics experience in product-based companies. We take interviews on a daily basis for our organization and we very well know what is asked in the interviews.
Other skill enhancer websites charge 2lakh+ GST for courses ranging from 10 to 15 months.
We only focus on making you a clear interview with ease. We have released our Become a Full Stack Analytics Professional for anyone in 2nd year of graduation to 8-10 YOE. This book contains 23 topics and each topic is divided into 50/100/200/250 questions and answers. Pick the book and read
it thrice, learn it, and appear in the interview.
We also have a complete Analytics interview package
– 2200 questions ebook (Rs.1999) + 23 ebook bundle for Data Science and Analyst role (Rs.1999)
– 4 one-hour mock interviews, every Saturday (top mate – Rs.1000 per interview)
– 4 career guidance sessions, 30 mins each on every Sunday (top mate – Rs.500 per session)
– Resume review and improvement (Top mate – Rs.500 per review)
Total cost – Rs.10500
Discounted price – Rs. 9000
How to avail of this offer?
Send a mail to [email protected]