Python Exercise13

#regular expressions
import re

txt=”You can call me on 9988888888. My ATM pin is 1234 . You are good”
#find all occurances of “ATM”
words=re.findall(“ATM”,txt)
print(“Occurances of ATM : “,words)

#find if the string begins with “You”
words=re.findall(“^You”,txt)
print(words)

#find if the string ends with “You”
words=re.findall(“You$”,txt)
print(words)

#find all digits in the string”
words=re.findall(“\d”,txt)
print(words)

#find one or more digits”
words=re.findall(“\d+”,txt)
print(words)

#find ATM pin in the string”
words=re.findall(“\d\d\d\d”,txt)
print(“ATM Pin”,words)

#find ATM pins in the string”
words=re.findall(“\s\d\d\d\d\s”,txt)
print(words)

#find ATM pins in the string”
words=re.findall(“\s\d{4}\s”,txt)
print(words)

#find ATM pins in the string”
words=re.findall(r”\b\d{4}\b”,txt)
print(words)

#find all ATM pin in the string and replace with ****
pin=re.sub(r”\b\d{4}\b”,”****”,txt)
print(“Pin: “,pin)

Be the first to comment

Leave a Reply

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