Regular Expressions in Python
Regular Expression (RegEx) is a sequence of characters that forms a search pattern. It is used for matching and
manipulating strings based on specific patterns — such as validating emails, finding phone numbers, splitting text,
etc.
Python Module for RegEx
Python provides a built-in module named 're' to work with regular expressions:
import re
Common Functions in re Module
Function Description
[Link]() Checks for a match only at the beginning
of the string
[Link]() Searches the entire string for a match
[Link]() Returns a list of all matches
[Link]() Returns an iterator yielding match objects
[Link]() Replaces one or many matches with a
string
[Link]() Splits a string by the occurrences of a
pattern
Common Regex Metacharacters
Pattern Meaning
. Matches any character except newline
^ Matches beginning of string
$ Matches end of string
* Matches 0 or more repetitions
+ Matches 1 or more repetitions
? Matches 0 or 1 repetition
{n} Matches exactly n repetitions
{n,} At least n repetitions
{n,m} Between n and m repetitions
[] Matches a set of characters
Character Classes
Pattern Meaning
\d Digit (0–9)
\D Not a digit
\w Word character (a-z, A-Z, 0–9, _)
\W Not a word character
\s Whitespace
\S Non-whitespace
+91 6305693431, 9154641231, 8790732332
MIG 214, 2ND FLOOR, KPHB KUKATPALLY, HYDERABAD, 500072 | SRA EDUTECH PVT LTD
Examples
[Link]()
import re
text = "My phone number is 9876543210"
match = [Link](r'\d{10}', text)
if match:
print("Found:", [Link]())
[Link]()
import re
text = "The price is 100 and 200 and 300"
numbers = [Link](r'\d+', text)
print(numbers)
[Link]()
import re
text = "Hello 123, this is 456"
new_text = [Link](r'\d+', '###', text)
print(new_text)
[Link]()
import re
result = [Link](r'Ajay', 'Ajay Kumar')
print([Link]())
+91 6305693431, 9154641231, 8790732332
MIG 214, 2ND FLOOR, KPHB KUKATPALLY, HYDERABAD, 500072 | SRA EDUTECH PVT LTD
Question 1:
Write a program to validate username and password using regex.
import re
n = input('Enter username: ')
res = [Link]('Ajay123', n)
if res:
print('Enter password***')
n2 = input('Enter Your password: ')
res2 = [Link]('ajay8499', n2)
if res2:
print('Login successful')
else:
print('Wrong password')
else:
print('Wrong username')
Question 2:
Extract all digits one by one from a given string.
import re
a = 'python full stack developer @ 123456'
data = [Link](r'\d', a)
for x in data:
print(x)
Question 3:
Find all digits from a given string.
import re
a = 'python full stack developer @ 123456'
data = [Link](r'\d', a) # ['1','2','3','4','5','6']
print(data)
Question 4:
Find all non-digit characters from a given string.
import re
a = 'python full stack developer @ 123456'
data = [Link](r'\D', a)
print(data)
Question 5:
Search for the first whitespace in a string.
import re
a = 'python full stack developer @ 123456'
data = [Link](r'\s', a)
print(data)
+91 6305693431, 9154641231, 8790732332
MIG 214, 2ND FLOOR, KPHB KUKATPALLY, HYDERABAD, 500072 | SRA EDUTECH PVT LTD
Question 6:
Find all vowels in a string.
import re
a = 'python'
data = [Link](r'[aeiou]', a)
print(data)
Question 7:
Extract all digits from a string that contains a mobile number.
import re
name = 'kumar mobile number 8499054206'
res = [Link](r'\d', name)
print(res)
Question 8:
Extract all non-digit characters (optional match) from a string.
import re
name = 'kumar mobile number 8499054206'
res = [Link](r'\D?', name)
print(res)
Question 9:
Validate a mobile number in India (should start with 6–9 and have 10 digits).
import re
n = input('Enter mobile number: ')
res = [Link](r'[6-9][0-9]{9}', n)
if res:
print('Valid mobile number')
else:
print('Invalid mobile number')
Summary
Task Regex
Match 3 digits \d{3}
Match word \w+
Match lowercase a to z [a-z]
Match anything .*
Match only start of string ^pattern
Match only end of string pattern$
+91 6305693431, 9154641231, 8790732332
MIG 214, 2ND FLOOR, KPHB KUKATPALLY, HYDERABAD, 500072 | SRA EDUTECH PVT LTD