Regular Expressions in Python
Regular Expressions in Python
11 print(my_string1+" "+my_string2)
str(123)
'123'
print(my_string[3])
Awe
print(my_string[:5])
print(my_string[5:])
print(my_string[-1])
Aweso
y
me day
my_string = "Awesome day"
print(my_string[0:6:2])
Aeo
print(my_string[::-1])
yad emosewA
print(my_string.lower())
print(my_string.upper())
print(my_string.capitalize())
['This', 'string', 'will be split']
my_string.strip()
my_list = ["this", "would", "be", "a", "string"]
print(" ".join(my_list))
'This string will be stripped'
my_string.rstrip()
my_string.lstrip()
my_string.find("Wenda")
-1
.find()
my_string.find("Waldo", 0, 6)
8
-1
my_string.index("Wenda")
my_string = "Where's Waldo?" my_string = "How many fruits do you have in your fruit basket?"
my_string.count("fruit")
try:
my_string.index("Wenda") 2
except ValueError:
print("Not found")
my_string.count("fruit", 0, 16)
"Not found"
1
my_string = "The red house is between the blue house and the old house"
print(my_string.replace("house", "car"))
The red car is between the blue car and the old car
The red car is between the blue car and the old house
custom_string = "String formatting"
print(f"{custom_string} is a powerful technique")
print(my_string.format(method, condition))
print("Machine learning provides {} the ability to learn {}".format("systems", "automatically")) Supervised algorithms rely on labeled datasets
print("{} has a friend called {} and a sister called {}".format("Betty", "Linda", "Daisy")) tool="Unsupervised algorithms"
goal="patterns"
print("{title} try to find {aim} in the dataset".format(title=tool, aim=goal))
Betty has a friend called Linda and a sister called Daisy
print("{2} has a friend called {0} and a sister called {1}".format("Betty", "Linda", "Daisy"))
way = "code"
method = "learning Python faster"
print(f"Practicing how to {way} is the best method for {method}")
Practicing how to code is the best method for learning Python faster
!s e
!r d
!a f
Python is called 'Python' due to a comedy series In the last 2 years, 90.42% of the data was produced worldwide!
family = {"dad": "John", "siblings": "Peter"}
datetime
print("Is your dad called {family[dad]}?".format(family=family))
4 multiplied by 7 is 28
from string import Template
my_string = Template('Data science has been called $identifier')
my_string.substitute(identifier="sexiest job of the 21st century")
'Data science has been called sexiest job of the 21st century'
$identifier ${identifier}
'Data science has been called sexiest job of the 21st century'
$$
favorite = dict(flavor="chocolate")
my_string = Template('I love $flavor $cake very much')
my_string = Template('I paid for the Python course only $$ $price, amazing!') my_string.substitute(favorite)
my_string.substitute(price="12.50")
favorite = dict(flavor="chocolate")
my_string = Template('I love $flavor $cake very much')
\d \s \w {3,10} \d \s \w {3,10}
\d \s \w {3,10}
import re
['#movies', '#movies']
import re import re
re.sub(r"yellow", "nice", "I have a yellow car and a yellow house in a yellow neighborhood")
re.split(r"!", "Nice Place to eat! I'll come back! Excellent meat!")
re.findall(r"User\d", "The winners are: User9, UserN, User8") re.findall(r"User\w", "The winners are: User9, UserN, User8")
re.findall(r"User\D", "The winners are: User9, UserN, User8") re.findall(r"\W\d", "This skirt is on sale, only $5 today!")
['UserN'] ['$5']
re.findall(r"Data\sScience", "I enjoy learning Data Science")
['Data Science']
<_sre.SRE_Match object; span=(0, 12), match='password1234'> <_sre.SRE_Match object; span=(0, 12), match='password1234'>
+ +
text = "Date of start: 4-3. Date of registration: 10-04." text = "Date of start: 4-3. Date of registration: 10-04."
+ *
text = "Date of start: 4-3. Date of registration: 10-04." my_string = "The concert was amazing! @ameli!a @joh&&n @mary90"
re.findall(r"@\w+\W*\w+", my_string)
re.findall(r"\d+-\d+", text)
['@ameli!a', '@joh&&n', '@mary90']
['4-3', '10-04']
? {n, m}
text = "The color of this image is amazing. However, the colour blue could be brighter." phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
re.findall(r"colou?r", text)
{n, m} {n, m}
phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424" phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
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_string = "the 80s music was much better that the 90s"
re.findall(r"the\s\d+s", my_string)
['www.amazingpics.com']
re.findall(r"^the\s\d+s", my_string)
['the 80s']
$ \
my_string = "the 80s music hits were much better that the 90s" my_string = "I love the music of Mr.Go. However, the sound was too loud."
['the 90s'] ['', '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" my_string = "Yesterday I spent my afternoon with my friends: MaryJohn2 Clary3"
['www.hola.com']
import re
* + ? {num, num} re.match(r"\d+", "12345bcada")
import re ?
re.match(r".*hello", "xhelloxxxxxx")
import re
re.match(r"\d+?", "12345bcada")
<_sre.SRE_Match object; span=(0, 6), match='xhello'>
re.findall(r'([A-Za-z]+)\s\w+\s\d+\s\w+', text)
r"apple+" +
pets = re.findall(r'([A-Za-z]+)\s\w+\s(\d+)\s(\w+)', "Clary has 2 dogs but John has 3 cats")
pets[0][0]
'Clary'
['5', '3']
re.findall(r"(\d+)", my_string)
['8755', '33']
my_string = "I want to have a pet. But I don't know if I want a cat, a dog or a bird."
re.findall(r"cat|dog|bird", my_string)
my_string = "I want to have a pet. But I don't know if I want 2 cats, 1 dog or a bird."
re.findall(r"\d+\scat|dog|bird", my_string)
my_string = "I want to have a pet. But I don't know if I want 2 cats, 1 dog or a bird."
['2 cat', 'dog', 'bird']
re.findall(r"\d+\s(cat|dog|bird)", my_string)
['cat', 'dog']
?: (?:regex)
my_string = "I want to have a pet. But I don't know if I want 2 cats, 1 dog or a bird."
re.findall(r"(\d)+\s(cat|dog|bird)", my_string)
['23', '24']
my_string = "John Smith: 34-34-34-042-980, Rebeca Smith: 10-10-10-434-425"
re.findall(r"(?:\d{2}-){3}(\d{3}-\d{3})", my_string)
['042-980', '434-425']
text = "Python 3.0 was released on 12-03-2008."
'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!" sentence = "I wish you a happy happy birthday!"
re.findall(r"(\w+)\s ", sentence) 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)
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)
['tweets.txt', 'mypass.txt']
my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error" my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"
re.findall(r"\w+\.txt ", my_text) re.findall(r"\w+\.txt(?!\stransferred)", my_text)
['keywords.txt']
my_text = "Member: Angus Young, Member: Chris Slade, Past: Malcolm Young, Past: Cliff Williams."
re.findall(r" \w+\s\w+", my_text)
my_text = "Member: Angus Young, Member: Chris Slade, Past: Malcolm Young, Past: Cliff Williams." my_text = "My white cat sat at the table. However, my brown dog was lying on the couch."
re.findall(r"(?<=Member:\s)\w+\s\w+", my_text) re.findall(r"(?<!brown\s)(cat|dog)", my_text)