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 Regular Expressions match() function in Python.
import re
pattern = r'^\d{4}-\d{3}-\d{4}$'
phone= '1800-456-7890'
#phone= '123-456-7890'
#phone = '123/456/7890'
if re.match(pattern, phone):
    print("Valid Phone Number")
else:
    print("Invalid Phone Number")

Output:
Valid Phone Number
Example : How to validate a String value with required pattern for E-mail using Regular Expressions match() function in Python.
import re
email = "[email protected]"
pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"

if re.match(pattern, email):
    print("Valid form of email as Pattern")
else:
    print("Invalid form of email as Pattern")

Output:
Valid form of email as Pattern
Example : How to divide a String value with required pattern using 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 required pattern using 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 Date with required pattern using 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 a all String value with required pattern using 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 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

Loading

Categories: Python Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.