Lookaround

Regular Expressions in Python

Maria Eugenia Inzaugarat

Data Scientist

Looking around

  • Allow us to confirm that sub-pattern is ahead or behind main pattern

   

Regular Expressions in Python

Looking around

  • Allow us to confirm that sub-pattern is ahead or behind main pattern

 

At my current position in the matching process, look ahead or behind and examine whether some pattern matches or not match before continuing.

Regular Expressions in Python

Look-ahead

  • Non-capturing group
  • Checks that the first part of the expression is followed or not by the lookahead expression
  • Return only the first part of the expression

Regular Expressions in Python

Positive look-ahead

  • Non-capturing group
  • Checks that the first part of the expression is followed by the lookahead expression
  • Return only the first part of the expression

 

my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"

re.findall(r"\w+\.txt ", my_text)
Regular Expressions in Python

Positive look-ahead

  • Non-capturing group
  • Checks that the first part of the expression is followed by the lookahead expression
  • Return only the first part of the expression

 

my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"

re.findall(r"\w+\.txt(?=\stransferred)", my_text)
['tweets.txt', 'mypass.txt']
Regular Expressions in Python

Negative look-ahead

  • Non-capturing group
  • Checks that the first part of the expression is not followed by the lookahead expression
  • Return only the first part of the expression

 

my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"

re.findall(r"\w+\.txt ", my_text)
Regular Expressions in Python

Negative look-ahead

  • Non-capturing group
  • Checks that the first part of the expression is not followed by the lookahead expression
  • Return only the first part of the expression

 

my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"

re.findall(r"\w+\.txt(?!\stransferred)", my_text)
['keywords.txt']
Regular Expressions in Python

Look-behind

  • Non-capturing group
  • Get all the matches that are preceded or not by a specific pattern.
  • Return pattern after look-behind expression

Regular Expressions in Python

Positive look-behind

  • Non-capturing group
  • Get all the matches that are preceded by a specific pattern.
  • Return pattern after look-behind expression

 

my_text = "Member: Angus Young, Member: Chris Slade, Past: Malcolm Young, Past: Cliff Williams."

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

Positive look-behind

  • Non-capturing group
  • Get all the matches that are preceded by a specific pattern.
  • Return pattern after look-behind expression

 

my_text = "Member: Angus Young, Member: Chris Slade, Past: Malcolm Young, Past: Cliff Williams."

re.findall(r"(?<=Member:\s)\w+\s\w+", my_text)
['Angus Young', 'Chris Slade']
Regular Expressions in Python

Negative look-behind

  • Non-capturing group
  • Get all the matches that are not preceded by a specific pattern.
  • Return pattern after look-behind expression

 

my_text = "My white cat sat at the table. However, my brown dog was lying on the couch."

re.findall(r"(?<!brown\s)(cat|dog)", my_text)
['cat']
Regular Expressions in Python

Let's practice!

Regular Expressions in Python

Preparing Video For Download...