How to Set up a Look-behind Regular Expression in Python



Python


In this article, we show how to set up a look-behind regular expression in Python.

A look-behind regular expression is a regular expression that looks behinds, or to the left of the elements of a string.

Let's say we have the following string, string1= "1)Coconut 2)Grapes 3)Cantaloupe"

What comes before each element?

The number followed by a closing parenthesis.

So looking behind, or to the left, of each element in the string is number followed by a closing parenthesis.

A look-behind regular expression returns the elements of a string in which an element is preceded by the expression in which we define.

In this case, we look behind each element for a number followed by a closing parenthesis.

The look-behind regular expression will not the number with the closing parenthesis. Rather, it returns the element that comes after this pattern. It looks behind each element for the pattern (number followed by a closing parenthesis) but does not return the pattern (the number followed by a closing parenthesis), but only the element.

So let's go over a working example to see how look-behind regular expressions are set up and how they work in Python.

This is shown in the following code below.



So let's now go over this code.

re is the module in Python that allows us to use regular expressions. So we first have to import re in our code, in order to use regular expressions.

After this, we have a variable, string1, which is a couple of elements, each preceded by a number in ascending order starting from 1 followed by a closing parenthesis.

We then have our regex variable, which is set equal to, re.compile(r"(?<=\d\))\w+")

?<= means that this is a look behind regular expression.

This regular expression looks for a digit (\d followed by a parentheses ( (\) ). This whole part is enclosed in parentheses

We then have, \w+, which looks for alphabetical characters in the elements.

We then check for all matches using the re.findall() function.

We then output the found matches.

This produces the element after each numerical number in the string.

Remember that look-behind regular expressions do not return the pattern you are matching (in this case a number followed by a closing parenthesis). It only returns the element after the pattern. The pattern is there just to show where to look for each element.

And this is all that is needed to set up a look-behind regular expression in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...