Table of Contents
hide
Example : How to remove Extra Space from a String using Regular Expressions sub() function in Python.
import re
str = 'Hello World India Python'
result = re.sub(r'\s+', ' ', str)
print(result)
Output:
Hello World India Python
-------------- OR --------------
import re
result = re.sub(r'\d+', 'My', 'Hello 123 World 456')
print(result)
Output:
Hello My World My
Example : How to Validate a Phone Number from a String value using the Regular Expression match() function in Python.
import re
phone= '+91-9876543210'
# pattern = r'^(\+91-)?\d{10}$'
# pattern = r'^\+91-\d{10}$'
# pattern = r'^\+91-\d[0-9]+$'
pattern = r'^[0-9+-]+\d[0-9]+$'
if re.match(pattern, phone):
print("Valid Phone Number")
else:
print("Invalid Phone Number")
----------------- OR ------------------
import re
phone= '1800-456-7890'
# phone= '1800/456/7890'
pattern = r'^\d[0-9]+-\d[0-9]+-\d[0-9]+$'
# pattern = r'^\d{4}-\d{3}-\d{4}$'
# pattern = r'^\d{4}/\d{3}/\d{4}$'
if re.match(pattern, phone):
print("Valid Phone Number")
else:
print("Invalid Phone Number")
Output:
Valid Phone Number
---------------- OR ------------------
import re
# text = 'abc_123'
# pattern = r'\w+'
# pattern = r'^\w+'
# pattern = r'^\w+$'
# text = '[email protected]'
# pattern = r'\w+'
# text = '[email protected]'
# pattern = r'^\w+'
# pattern = r'^\w+$'
text = 'user_0123_admin'
pattern = r'^\w{5,20}$'
pattern = r'^\w{5,}$'
if re.match(pattern, text):
print("Valid text")
else:
print("Invalid text")
Output:
Valid text
Example : How to validate a String value with the required pattern for Email using the Regular Expressions match() function in Python.
import re
# email = "[email protected]"
# pattern = r"^[a-z_0-9]+@[a-z0-9]+\.[a-z]+$"
# email = "[email protected]"
# pattern = r"^[a-z_0-9A-Z]+@[a-z0-9-]+\.[a-z]+$"
# pattern = r"^[_a-z0-9A-Z]+@[a-z0-9-]+\.[a-z]+$"
# pattern = r"^[_0-9A-Za-z]+@[a-z0-9-]+\.[a-z]+$"
# email = "[email protected]"
# pattern = r"^[a-z_0-9]+@[a-z]+\.[a-z]+$"
# email = "[email protected]"
# pattern = r"^[a-z_$0-9]+@[a-z]+\.[a-z]+\.[a-z]+$"
# email = "[email protected]"
# pattern = r"^[_0-9a-zA-Z]+@[a-zA-Z0-9]+\.[a-z]+$"
# email = "[email protected]"
# pattern = r"^[a-z.1_A-Z]+@[A-Za-z0-9]+\.[a-z]+$"
# email = "[email protected]"
# pattern = r"^[a-z.0-9@]+$"
# email = "[email protected]"
# pattern = r"^[_0-9a-zA-Z]+@[a-zA-Z0-9]+\.[a-z]{3}$"
# email = "[email protected]"
# pattern = r"^[_0-9a-zA-Z]+@[a-zA-Z0-9]+\.[a-z]{2,}$"
# email = "[email protected]"
# pattern = r"^[_0-9a-zA-Z]+\$\$[A-Z]+@[a-zA-Z0-9]+\.[a-z]{2,}$"
# pattern = r"^[0-9_a-zA-Z$]+@[a-zA-Z0-9]+\.[a-z]{2,}$"
if re.match(pattern, email):
print("Valid form of email as Pattern")
else:
print("Invalid form of email as Pattern")
# if re.fullmatch(pattern, email):
# print("Valid form of email as Pattern")
# else:
# print("Invalid form of email as Pattern")
Output:
Valid form of email as Pattern
NB:
Here,
[i] ([a-z]), denotes a character set/range (all lowercase letters from a to z) of characters but match any one character from this range only whereas ([a-z]+) denotes a character set/range (all lowercase letters from a to z) of characters but match one or more character from this range only. similarly,([a-z]*) denotes a character set/range (all lowercase letters from a to z) of characters but match zero or more character from this range only.
[ii] Hyphen(-) when placed at the start or end of a character set, it is treated as a literal hyphen and not a range indicator.
[iii] To use keyword pattern symbols having special meaning in our regular expressions such as ., $,+,* etc. as single symbol (i.e., not inside []) then the \ followed by that symbol (\.,\$,\* etc) that will represent a literal .,$,* in the expression. this is because ., +, $, * etc. has a special meaning in regular expression (it matches any character), hence we use a backslash (\) to escape it and match it as a literal only.
Example : How to divide a String value with the required pattern using the Regular Expressions split() function in Python.
import re
str = re.split(r"\s+", "Python is a Future Programming Language")
print(str)
Output:
['Python', 'is', 'a', 'Future', 'Programming', 'Language']
-------------- OR --------------
import re
str = re.split(r"\s+", "Python\nis\na\nFuture\nProgr\namming\nLang\nuage")
print(str)
Output:
['Python', 'is', 'a', 'Future', 'Progr', 'amming', 'Lang', 'uage']
-------------- OR --------------
import re
result = re.split(r'\d+', 'My12India56is541Great')
print(result)
Output:
['My', 'India', 'is', 'Great']
Example : How to display all String values with the required pattern using the Regular Expressions findall() function in Python.
import re
result = re.findall(r"\d+", "My five Subjects marks are 84 90 78 69 and 58")
print(result)
Output:
['84', '90', '78', '69', '58']
Example : How to display all String values as dates with the required pattern using the Regular Expressions findall() function in Python.
import re
pattern = r'\d{2}-\d{2}-\d{4}'
text = 'Yesterday was 02-02-2025 Today is 03-02-2025 and tomorrow is 04-02-2025.'
dates = re.findall(pattern, text)
print(dates)
Output:
['02-02-2025', '03-02-2025', '04-02-2025']
Example : How to display all String values with the required pattern using the Regular Expressions finditer() function in Python.
import re
for match in re.finditer(r"\d+", "My five Subjects marks are 84 90 78 69 and 58"):
print(match.group())
Output:
84
90
78
69
58
Example : How to match a String value with a required pattern using Regular Expressions using Flag RegEx in Python.
import re
text = "Hello world"
match = re.search(r"hello", text, re.I)
print(bool(match))
Output:
True
-------------- OR --------------
import re
text = "Hello world"
match = re.search(r"hello", text)
print(bool(match))
Output:
False
-------------- OR --------------
import re
text = "Hello world"
match = re.search(r"hello", text)
print((match))
Output:
False
![]()
0 Comments