Positive lookahead assertion

Carl Banks pavlovevidence at gmail.com
Thu Sep 7 22:45:17 EDT 2006


tobiah wrote:
> Sorry, I should have tried harder.  I see that the text
> of the match is simply not consumed, so that:
>
> 	m = re.search('(?=foo)fo', 'food')
>
> succeeds, while
>
> 	m = re.search('(?=fox)fo', 'food')
>
> does not.

They are more commonly used, and generally more useful, at the end of a
regexp:

m = re.search(r"foo(?=d)","food")

matches, but afterwards m.group(0)=="foo" (without the d).  Meanwhile,

m = re.search(r"foo(?=d)","fool")

doesn't match at all.

Carl Banks




More information about the Python-list mailing list