Confused about 'positive lookbehind assertion'

Andrew Durdin adurdin at gmail.com
Tue Sep 25 18:01:06 EDT 2007


On 9/25/07, Karthik Gurusamy <kar1107 at gmail.com> wrote:
>
> Any idea what this positive lookbehind achieves which can't be done
> without it.
> I remember cases where positive look-ahead is useful.
>
> In the above example, r.search('abcdef') does the job of ensuring
> 'def' is preceded by 'abc'.

AFAICT the only benefit I can see is that the lookbehind isn't
captured, so (a) it's not included in match.group(n), and (b)
match.start(), match.end(), and match.span() return the offsets of the
bit you actually wanted to capture.

OTOH, I often find the negative lookbehind (?!...) very useful indeed.

Andrew



More information about the Python-list mailing list