Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex error: look-behind requires fixed-width pattern

The following regex is supposed to match any :text: that's preceeded by start-of-string, whitespace or :, and succeeded by end-of-string, whitespace or : (Along with a few extra rules)

I'm not great at regex but I've come up with the desired solution in regexr.com:

(?<=\s|:|^)(:[^\s|:]+:)(?=\s|:|$)
:match1::match2: :match3:
:match4:
000:matchNot:
:matchNot:000
:match Not:

Result: :match1:, :match2:, :match3:, :match4:

But on Python 3 this raises an error.

re.search("(?<=\s|:|^)(:[^\s|:]+:)(?=\s|:|$)", txt)

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

Anyone know a good workaround for this issue? Any tips are appreciated.

like image 730
Nayncore Avatar asked May 11 '26 09:05

Nayncore


1 Answers

Possibly the easiest solution would be to use the newer regex module which supports infinite lookbehinds:

import regex as re

data = """:match1::match2: :match3:
:match4:
000:matchNot:
:matchNot:000
:match Not:"""

for match in re.finditer("(?<=\s|:|^)(:[^\s|:]+:)(?=\s|:|$)", data):
    print(match.group(0))

This yields

:match1:
:match2:
:match3:
:match4:
like image 130
Jan Avatar answered May 20 '26 01:05

Jan