Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#eg1
import re
string = "Lotus is the national flower of my country"
pattern = "Lotus"
ex1 = re.match(pattern, string)
print(ex1)
#eg2
pattern2 = "Rose"
ex2 = re.match(pattern2, string)
print(ex2)
#eg3
pattern3 ="my"
ex3 = re.search(pattern3, string)
print(ex3)
#eg4
string1= "Lotus is the national flower of my country and it's national animal is tiger"
pattern4 = "is"
ex4 = re.findall(pattern4, string1)
print(ex4)
#eg5
ex4 = re.finditer(pattern4, string1)
for a in ex4:
print(a.start())
#eg6
string_2 = "Rashi was born on 05-02-1999 and she was admitted on 20-04-2002"
pattern5 = "\d{2}-\d{2}\d{2}"
ex5 = re.findall(pattern5, string_2)
print(ex5)
#eg7
pattern6 = "Rashi"
replacement1 = "Himanshi"
ex6 = re.sub(pattern6, replacement1, string_2)
print(ex6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment