anupamGak
anupamGak

Reputation: 95

lookbehind for start of string or a character

The command

re.compile(ur"(?<=,| |^)(?:next to|near|beside|opp).+?(?=,|$)", re.IGNORECASE)

throws a

sre_constants.error: look-behind requires fixed-width pattern

error in my program but regex101 shows it to be fine.

What I'm trying to do here is to match landmarks from addresses (each address is in a separate string) like:

The lookbehind is to avoid matching words with opp in them (like in string 3).

Why is that error thrown? Is there an alternative to what I'm looking for?

Upvotes: 9

Views: 3039

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174874

Use re.findall with the below regex, since re.findall must return the contents insdie the capturing group if there is any capturing group presents.

re.compile(ur"(?m)(?:[, ]|^)((?:next to|near|beside|opp).+?)(?:,|$)", re.IGNORECASE)

Upvotes: 0

vks
vks

Reputation: 67998

re.compile(ur"(?:^|(?<=[, ]))(?:next to|near|beside|opp).+?(?=,|$)", re.IGNORECASE)

You can club 3 conditions using [] and |.See demo.

https://regex101.com/r/vA8cB3/2#python

Upvotes: 9

Related Questions