0% found this document useful (0 votes)
11 views

Regular Expressions in Python

The document contains various examples of string manipulation and formatting in Python. It covers topics such as string methods, regular expressions, and formatting techniques using f-strings and the Template class. The examples illustrate how to manipulate strings, search for patterns, and format output effectively.

Uploaded by

danndanium
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Regular Expressions in Python

The document contains various examples of string manipulation and formatting in Python. It covers topics such as string methods, regular expressions, and formatting techniques using f-strings and the Template class. The examples illustrate how to manipulate strings, search for patterns, and format output effectively.

Uploaded by

danndanium
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

my_string = "This is a string"

my_string2 = 'This is also a string'

my_string = 'And this? It's the wrong string'

my_string = "And this? It's the correct string"


+

my_string = "Awesome day" my_string1 = "Awesome day"


len(my_string) my_string2 = "for biking"

11 print(my_string1+" "+my_string2)

Awesome day for biking

str(123)

'123'

my_string = "Awesome day" my_string = "Awesome day"


print(my_string[0:3])

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

my_string = "tHis Is a niCe StriNg"

print(my_string.lower())

this is a nice string

print(my_string.upper())

THIS IS A NICE STRING


my_string = "tHis Is a niCe StriNg" my_string = "This string will be split"

my_string.split(sep=" ", maxsplit=2)

print(my_string.capitalize())
['This', 'string', 'will be split']

This is a nice string


my_string.rsplit(sep=" ", maxsplit=2)

['This string will', 'be', 'split']

my_string = "This string will be split\nin two"


my_string = "This string will be split\nin two"
print(my_string)

This string will be split


my_string.splitlines()
in two

['This string will be split', 'in two']


.strip()

my_string = " This string will be stripped\n"

my_string.strip()
my_list = ["this", "would", "be", "a", "string"]
print(" ".join(my_list))
'This string will be stripped'

this would be a string

my_string = " This string will be stripped\n"

my_string.rstrip()

' This string will be stripped'

my_string.lstrip()

'This string will be stripped\n'


my_string = "Where's Waldo?"
my_string.find("Waldo")

my_string.find("Wenda")

-1

.find()

my_string = "Where's Waldo?" my_string = "Where's Waldo?"


my_string.index("Waldo")

my_string.find("Waldo", 0, 6)
8

-1
my_string.index("Wenda")

File "<stdin>", line 1, in <module>


ValueError: substring not found
.find()

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

print(my_string.replace("house", "car", 2))

The red car is between the blue car and the old house
custom_string = "String formatting"
print(f"{custom_string} is a powerful technique")

String formatting is a powerful technique


my_string = "{} rely on {} datasets"
method = "Supervised algorithms"
condition = "labeled"
str.format()

print(my_string.format(method, condition))

print("Machine learning provides {} the ability to learn {}".format("systems", "automatically")) Supervised algorithms rely on labeled datasets

Machine learning provides systems the ability to learn automatically

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

Unsupervised algorithms try to find patterns in the dataset

print("{2} has a friend called {0} and a sister called {1}".format("Betty", "Linda", "Daisy"))

Daisy has a friend called Betty and a sister called Linda


my_methods = {"tool": "Unsupervised algorithms", "goal": "patterns"} {index:specifier}

print("Only {0:f}% of the {1} produced worldwide is {2}!".format(0.5155675, "data", "analyzed"))


print('{data[tool]} try to find {data[goal]} in the dataset'.format(data=my_methods))

Only 0.515568% of the data produced worldwide is analyzed!


Unsupervised algorithms try to find patterns in the dataset

print("Only {0:.2f}% of the {1} produced worldwide is {2}!".format(0.5155675, "data", "analyzed"))

Only 0.52% of the data produced worldwide is analyzed!

from datetime import datetime


print(datetime.now())

datetime.datetime(2019, 4, 11, 20, 19, 22, 58582)

print("Today's date is {:%Y-%m-%d %H:%M}".format(datetime.now()))

Today's date is 2019-04-11 20:20


f

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

name = "Python" number = 90.41890417471841


print(f"Python is called {name!r} due to a comedy series") print(f"In the last 2 years, {number:.2f}% of the data was produced worldwide!")

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))

Is your dad called John?


from datetime import datetime
my_today = datetime.now()

print(f"Today's date is {my_today:%B %d, %Y}") family["dad"]

print(f"Is your dad called {family[dad]}?")


Today's date is April 14, 2019

NameError: name 'dad' is not defined

\ family = {"dad": "John", "siblings": "Peter"}

print("My dad is called "John"")

SyntaxError: invalid syntax print(f"Is your dad called {family[\"dad\"]}?")

SyntaxError: f-string expression part cannot include a backslash

my_string = "My dad is called \"John\""

My dad is called "John" print(f"Is your dad called {family['dad']}?")

Is your dad called John?


def my_function(a, b):
return a + b
my_number = 4
my_multiplier = 7
print(f"If you sum up 10 and 20 the result is {my_function(10, 20)}")

print(f'{my_number} multiplied by {my_multiplier} is {my_number * my_multiplier}')


If you sum up 10 and 20 the result is 30

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}

from string import Template


my_string = Template('I find Python very ${noun}ing but my sister has lost $noun')
job = "Data science"
my_string.substitute(noun="interest")
name = "sexiest job of the 21st century"
my_string = Template('$title has been called $description')
my_string.substitute(title=job, description=name) 'I find Python very interesting but my sister has lost interest'

'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")

Traceback (most recent call last):


'I paid for the Python course only $ 12.50, amazing!' KeyError: 'cake'

favorite = dict(flavor="chocolate")
my_string = Template('I love $flavor $cake very much')

try: favorite = dict(flavor="chocolate")


my_string.substitute(favorite) my_string = Template('I love $flavor $cake very much')
except KeyError: my_string.safe_substitute(favorite)
print("missing information")

'I love chocolate $cake very much'


missing information
str.format()
st \d \s \w {3,10}

\d \s \w {3,10} \d \s \w {3,10}
\d \s \w {3,10}

import re

re.findall(r"#movies", "Love #movies! I had fun yesterday going to the #movies")

['#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!")

'I have a nice car and a nice house in a nice neighborhood'


['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")

['User9', 'User8'] ['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']

re.sub(r"ice\Scream", "ice cream", "I really like ice-cream")

'I really like ice cream'


import re import re
password = "password1234" password = "password1234"

re.search(r"\w\w\w\w\w\w\w\w\d\d\d\d", password) re.search(r"\w{8}\d{4}", password)

<_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."

re.findall(r" ", text) re.findall(r"\d+- ", text)

+ *

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)

re.findall(r" ", phone_number)


['color', 'colour']

{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}- ", phone_number) re.findall(r"\d{1,2}-\d{3}- ", phone_number)


{n, m}
r"apple+" +

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)

re.search(r"\d+", "Yesterday, I saw 3 shows") re.match(r"\d+","Yesterday, I saw 3 shows")

<_sre.SRE_Match object; span=(17, 18), match='3'> None

. ^

my_string = "the 80s music was much better that the 90s"

re.findall(r"the\s\d+s", my_string)

['the 80s', 'the 90s']


my_links = "Just check out this link: www.amazingpics.com. It has amazing photos!"
re.findall(r"www.+com", my_links)

['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."

re.findall(r"the\s\d+s$", my_string) print(re.split(r".\s", my_string))

['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"

re.findall(r"Elephant|elephant", my_string) re.findall(r"[a-zA-Z]+\d", my_string)

['Elephant', 'elephant'] ['MaryJohn2', 'Clary3']


[ ] [ ]
^
my_string = "My&name&is#John Smith. I%live$in#London."

re.sub(r"[#$%&]", " ", my_string)


my_links = "Bad website: www.99.com. Favorite site: www.hola.com"
re.findall(r"www[^0-9]+com", my_links)
'My name is John Smith. I live in London.'

['www.hola.com']
import re
* + ? {num, num} re.match(r"\d+", "12345bcada")

<_sre.SRE_Match object; span=(0, 5), match='12345'>

import re ?
re.match(r".*hello", "xhelloxxxxxx")
import re
re.match(r"\d+?", "12345bcada")
<_sre.SRE_Match object; span=(0, 6), match='xhello'>

<_sre.SRE_Match object; span=(0, 1), match='1'>


import re
re.match(r".*?hello", "xhelloxxxxxx")

<_sre.SRE_Match object; span=(0, 6), match='xhello'>


re.findall(r'[A-Za-z]+\s\w+\s\d+\s\w+', text)

['Clary has 2 friends', 'Susan has 3 brothers', 'John has 4 sisters']

re.findall(r'([A-Za-z]+)\s\w+\s\d+\s\w+', text)

['Clary', 'Susan', 'John']


re.findall(r'([A-Za-z]+)\s\w+\s(\d+)\s(\w+)', text)

[('Clary', '2', 'friends'),


('Susan', '3', 'brothers'),
('John', '4', 'sisters')]

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'

re.search(r"(\d[A-Za-z])+", "My user name is 3e4r5fg")

<_sre.SRE_Match object; span=(16, 22), match='3e4r5f'>


(\d+) (\d)+

my_string = "My lucky numbers are 8755 and 33"


re.findall(r"(\d)+", my_string)

['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)

['cat', 'dog', 'bird']


|

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)

[('2', 'cat'), ('1', 'dog')]


my_date = "Today is 23rd May 2019. Tomorrow is 24th May 19."
re.findall(r"(\d+)(?:th|rd)", my_date)

['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."

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!" 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)

'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.'


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)

['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)

['Angus Young', 'Chris Slade'] ['cat']

You might also like