Logo

Developer learning path

Python

Lookarounds in Python

Lookarounds

71

#description

Lookarounds refer to a concept in regular expressions in Python that allows you to look ahead or behind of a pattern without actually including it in the match. In other words, it's a way to test for a pattern match without actually consuming the characters involved.

Lookarounds are divided into two types: Positive lookaround and negative lookaround.

Positive lookaround is written as (?=pattern) or (?<=pattern), where pattern is the pattern you want to match. The first syntax is called a positive lookahead, and it matches any text that is followed by the pattern. The second syntax is called positive lookbehind, and it matches any text that is before the pattern.

Negative lookaround, on the other hand, is written as (?!pattern) or (?<!pattern). It negates the pattern match, meaning that it matches any text that is not followed or not preceded by the pattern.

Here's an example:

                    
import re

text = "Python is awesome!"

# Positive lookahead
result = re.search(r"Python(?= is)", text)
print(result.group()) # Output: Python

# Positive lookbehind
result = re.search(r"(?<=Python )is", text)
print(result.group()) # Output: is

# Negative lookahead
result = re.search(r"Python(?! is)", text)
print(result.group()) # Output: Python

# Negative lookbehind
result = re.search(r"(?<!awesome!)Python", text)
print(result.group()) # Output: Python
                  

In the example above, we used lookarounds to match specific patterns in a given text without actually including them in the match. This can be particularly useful when you want to match a pattern only if it's surrounded by specific elements or substrings.

March 25, 2023

If you don't quite understand a paragraph in the lecture, just click on it and you can ask questions about it.

If you don't understand the whole question, click on the buttons below to get a new version of the explanation, practical examples, or to critique the question itself.