Regular Expressions in Python
Maria Eugenia Inzaugarat
Data Scientist
text = "Python 3.0 was released on 12-03-2008."
information = re.search('(\d{1,2})-(\d{2})-(\d{4})', text)
information.group(3)
'2008'
information.group(0)
'12-03-2008'
text = "Austin, 78701"
cities = re.search(r"(?P<city>[A-Za-z]+).*?(?P<zipcode>\d{5})", text)
cities.group("city")
'Austin'
cities.group("zipcode")
'78701'
sentence = "I wish you a happy happy birthday!"
re.findall(r"(\w+)\s ", sentence)
sentence = "I wish you a happy happy birthday!"
re.findall(r"(\w+)\s\1", sentence)
['happy']
sentence = "I wish you a happy happy birthday!"
re.sub(r"(\w+)\s\1", r"\1", sentence)
'I wish you a happy birthday!'
sentence = "Your new code number is 23434. Please, enter 23434 to open the door."
re.findall(r"(?P<code>\d{5}).*?(?P=code)", sentence)
['23434']
sentence = "This app is not working! It's repeating the last word word."
re.sub(r"(?P<word>\w+)\s(?P=word)", r"\g<word>", sentence)
'This app is not working! It's repeating the last word.'
Regular Expressions in Python