Program [1]:
import re
pattern = r'\d+'
string='i have 123 apples and 456 oranges'
matches=re.findall(pattern, string)
print("Matches found:", matches)
Output:Matches found: ['123', '456']
Program [2]:
import re
pattern = r'\s+'
string = 'This is a sentence with some extra spaces.'
substrings=re.split(pattern, string)
print("Substring:", substrings)
print("Substrings:", substrings)
Output: Substring: [‘This’, ‘is’, ‘a’, ‘sentence’, ‘with’, ‘some’, ‘extra’, ‘spaces.’]
Substrings: ['This', 'is', 'a', 'sentence', 'with', 'some', 'extra', 'spaces.']
Program [3]:
import re
pattern = r'\W+'
string = 'Hello, World! How are you today?'
words = re.split(pattern, string)
print("Words Found:", words)
Output: Words Found: [‘Hello’, ‘World’, ‘How’, ‘are’, ‘you’, ‘today’, ”]
Program [4]:
import re
str="at what time?"
match = re.split('z', str)
print(match)
Output: [‘at what time?’]
Program [5]:
import re
pattern = r'hello'
string = 'hello world'
match_obj=re.match(pattern, string)
if match_obj:
print("Match found:", match_obj.group())
else:
print("No match found")
Output: Match found: hello
Program [6]:
import re
pattern = r'\d+'
string = 'I have 123 apples and 456 oranges'
matches = re.findall(pattern, string)
print("Matches found:", matches)
Output:Matches found: [‘123’, ‘456’]
Program [7]:
import re
pattern = r'\s+'
string = 'This is a sentence with some extra spaces.'
substrings = re.split(pattern, string)
print("SubstringS:", substrings)
Output: SubstringS: [‘This’, ‘is’, ‘a’, ‘sentence’, ‘with’, ‘some’, ‘extra’, ‘spaces.’]
Program [8]:
import re
pattern = r'\W+'
string = 'Hello, World! How are you today?'
words = re.split(pattern, string)
print("Words found:", words)
Output: Words found: [‘Hello’, ‘World’, ‘How’, ‘are’, ‘you’, ‘today’, ”]
Program [9]:
import re
str = "at what time?"
match = re.split('z', str)
print(match)
Output: [‘at what time?’]
Program [10]:
import re
pattern = r'\s+'
string = 'The quick brown for jumps over the lazy dog'
replacement ='-'
new_string = re.sub(pattern, replacement, string)
print("New string:", new_string)
Output: New string: The-quick-brown-for-jumps-over-the-lazy-dog
Program [11]:
import re
string = 'programming'
pattern = r'm'
matches = re.findall(pattern, string)
count = len(matches)
if count>1:
print("The letter 'm' is present in the string for more than once")
print(f"It appears {count} times in the string")
else:
print("The letter 'm' is not present in the string for more than once")
Output: The letter ‘m’ is present in the string for more than once
It appears 2 times in the string
Program [12]:
import re
string = 'Hello World!'
pattern = r'^[^!]*![^!]*$'
match = re.search(pattern, string)
if match:
print("The exclamation mark '!' is present in the string for exactly one time")
else:
print("The exclamation mark '!' is not present in the string for exactly one time")
Output: The exclamation mark ‘!’ is present in the string for exactly one time
Program [13]:
import re
integer = input ("Please enter a number:")
regex = r"^[+-]?\d+$"
if re.match(regex, integer):
print("Valid integer")
else:
print("Invalid integer")
Output: Please enter a number:+6789r452
Invalid integer
Program [14]:
import re
float_num = input ("Please enter a float number:")
regex = r"^[+-]?\d+$"
if re.match(regex, float_num):
print("Valid floating point number")
else:
print("Invalid floating point number")
Output: Please enter a float number:567.89e
Invalid floating point number
Program [15]:
import re
string = input ("Please enter a string:")
regex = r"^[a-zA-Z\s]+$"
if re.match(regex, string):
print("Valid string")
else:
print("Invalid string")
Output: Please enter a string:HEllo World WelCOME
Valid string
Program [16]:
import re
password = input(“Enter password to test:”)
pattern=r”^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$”
if re.match(pattern, password):
print(“Valid password”)
else:
print(“Invalid password”)
password = input(“Enter password to test:”)
pattern=r”^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$”
if re.match(pattern, password):
print(“Valid password”)
else:
print(“Invalid password”)
Output:
Enter password to test:Tray@12345 Valid password FAQ's: