regex - 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 program regex101 shows fine.

what i'm trying here match landmarks addresses (each address in separate string) like:

  • "opp foobar, foocity" --> must match "opp foobar"
  • "fooplace, near barplace, barcity" --> must match "near barplace"
  • "fooplace, shoppers stop, foocity"--> must match nothing
  • "fooplace, opp barplace"--> must match "opp barplace"

the lookbehind avoid matching words opp in them (like in string 3).

why error thrown? there alternative i'm looking for?

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

you can club 3 conditions using [] , |.see demo.

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


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -