[Python-Dev] Zero-width matching in regexes
Serhiy Storchaka
storchaka at gmail.com
Wed Dec 6 06:58:27 EST 2017
05.12.17 01:21, MRAB пише:
> I've finally come to a conclusion as to what the "correct" behaviour of
> zero-width matches should be: """always return the first match, but
> never a zero-width match that is joined to a previous zero-width match""".
>
> If it's about to return a zero-width match that's joined to a previous
> zero-width match, then backtrack and keep on looking for a match.
Isn't this how sub(), findall() and finditer() work in regex with
VERSION1? I agree that this behavior looks most logical and self-consistent.
Unfortunately the different behavior of re.sub() is documented explicitly:
"Empty matches for the pattern are replaced only when not adjacent to a
previous match, so sub('x*', '-', 'abc') returns '-a-b-c-'."
And there a special purposed test for this. One time the behavior was
changed when the re implementation was changed from pre to sre, but the
older behavior was restored. [1] [2]
[1] https://bugs.python.org/issue462270
[2]
https://github.com/python/cpython/commit/21009b9c6fc40b25fcb30ee60d6108f235733e40
More information about the Python-Dev
mailing list