Introduction To Regular Expressions: Maria Eugenia Inzaugarat
Introduction To Regular Expressions: Maria Eugenia Inzaugarat
regular expressions
REGULAR EXPRESSIONS IN PYTHON
Validate strings
['#movies', '#movies']
['Nice Place to eat', " I'll come back", ' Excellent meat', '']
re.sub(r"yellow", "nice", "I have a yellow car and a yellow house in a yellow neighborhood")
['User9', 'User8']
['UserN']
['$5']
['Data Science']
re.search(r"\w\w\w\w\w\w\w\w\d\d\d\d", password)
re.search(r"\w{8}\d{4}", password)
Quanti ers:
re.findall(r"\d+-\d+", text)
['4-3', '10-04']
text = "The color of this image is amazing. However, the colour blue could be brighter."
re.findall(r"colou?r", text)
['color', 'colour']
re.findall(r"\d{1,2}-\d{3}-\d{2,3}-\d{4,}", phone_number)
['1-966-847-3131', '54-908-42-42424']
re.search(r"\d{4}", "4506 people attend the show") re.match(r"\d{4}", "4506 people attend the show")
<_sre.SRE_Match object; span=(0, 4), match='4506'> <_sre.SRE_Match object; span=(0, 4), match='4506'>
my_links = "Just check out this link: www.amazingpics.com. It has amazing photos!"
re.findall(r"www com", my_links)
my_links = "Just check out this link: www.amazingpics.com. It has amazing photos!"
re.findall(r"www.+com", my_links)
['www.amazingpics.com']
my_string = "the 80s music was much better that the 90s"
re.findall(r"the\s\d+s", my_string)
re.findall(r"^the\s\d+s", my_string)
['the 80s']
my_string = "the 80s music hits were much better that the 90s"
re.findall(r"the\s\d+s$", my_string)
['the 90s']
my_string = "I love the music of Mr.Go. However, the sound was too loud."
print(re.split(r".\s", my_string))
['', 'lov', 'th', 'musi', 'o', 'Mr.Go', 'However', 'th', 'soun', 'wa', 'to', 'loud.']
print(re.split(r"\.\s", my_string))
['I love the music of Mr.Go', 'However, the sound was too loud.']
my_string = "Elephants are the world's largest land animal! I would love to see an elephant one day"
re.findall(r"Elephant|elephant", my_string)
['Elephant', 'elephant']
re.findall(r"[a-zA-Z]+\d", my_string)
['MaryJohn2', 'Clary3']
['www.hola.com']
Non-greedy or lazy
import re
re.match(r"\d+", "12345bcada")
import re
re.match(r".*hello", "xhelloxxxxxx")
import re
re.match(r"\d+?", "12345bcada")
import re
re.match(r".*?hello", "xhelloxxxxxx")