Backreferences

Regular Expressions in Python

Maria Eugenia Inzaugarat

Data Scientist

Numbered groups

 

 

Regular Expressions in Python

Numbered groups

 

Regular Expressions in Python

Numbered groups

 

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'
Regular Expressions in Python

Named groups

  • Give a name to groups

 

Regular Expressions in Python

Named groups

  • Give a name to groups
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'
Regular Expressions in Python

Backreferences

  • Using capturing groups to reference back to a group

 

Regular Expressions in Python

Backreferences

  • Using numbered capturing groups to reference back

 

sentence = "I wish you a happy happy birthday!"

re.findall(r"(\w+)\s ", sentence)
Regular Expressions in Python

Backreferences

  • Using numbered capturing groups to reference back

 

sentence = "I wish you a happy happy birthday!"

re.findall(r"(\w+)\s\1", sentence)
['happy']
Regular Expressions in Python

Backreferences

  • Using numbered capturing groups to reference back

 

sentence = "I wish you a happy happy birthday!"

re.sub(r"(\w+)\s\1", r"\1", sentence)
'I wish you a happy birthday!'
Regular Expressions in Python

Backreferences

  • Using named capturing groups to reference back

 

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']
Regular Expressions in Python

Backreferences

  • Using named capturing groups to reference back

 

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

Let's practice!

Regular Expressions in Python

Preparing Video For Download...