Raunakmalkani 20BIT032 Assignment RegularExpressions
Raunakmalkani 20BIT032 Assignment RegularExpressions
UID:- 20BIT032
REGEX ASSIGNMENT
METCHARACTERS:
SETS:
import re
def text_match(text):
patterns = 'ab+'
if re.search(patterns, text):
return 'Found a match'
else:
return 'Match not found'
print(text_match("ac"))
print(text_match("ab"))
print(text_match("abc"))
Output:-
def text_match(text):
patterns = 'ab{2,3}'
if re.search(patterns, text):
return 'Found a match'
else:
return 'Match not found'
print(text_match("ac"))
print(text_match("abbbb"))
print(text_match("abc"))
Output:-
def text_match(text):
patterns = 'a.*?b$'
if re.search(patterns, text):
return 'Found a match'
else:
return 'Match not found'
print(text_match("aaabbbd"))
print(text_match("abbbb"))
print(text_match("accddbbjjjb"))
Output:-
def text_match(text):
patterns = '^\w+'
if re.search(patterns, text):
return 'Found a match'
else:
return 'Match not found'
print(text_match(" The quick brown fox jumps over the lazy
dog."))
print(text_match("The quick brown fox jumps over the lazy
dog."))
Output:-
for n in results:
print(n.group(0))
Output:-
7. Write a Python program to search a literals string in a string
and also find the location within the original string where the
pattern occurs.
Input:-
import re
pattern = 'was'
text = 'The train was late.'
match = re.search(pattern, text)
s = match.start()
e = match.end()
Output:-
8. Write a Python program that count the words and spaces in a
string.
Input:-
import re
def string():
sen = str(input('Enter any string:'))
regex = r"(\w+)"
reex = r"(\s)"
Output:-