Positive lookahead assertion

Steve Holden steve at holdenweb.com
Fri Sep 8 04:11:42 EDT 2006


tobiah wrote:
>>>Posted via a free Usenet account from http://www.teranews.com
>>
>>Its all about context. If you want to match something but only if it
>>precedes something else, then you follow the regular expression for
>>'something' by the regular expression for 'something else' where
>>`something else` is enclosed by (?=...)
>>
>>The regular expression engine will surreptitiously check that
>>'something else' does indeed follow, before returning any match of
>>'something'.
> 
> 
> 
> How would this differ from just
> 
> re.search('somethingsomething else')
> 
The difference is only significant if your pattern contains groups that 
should be available after the match is complete, or if the match is 
folloed by further match elements. A lookahead assertion does precisely 
that: it looks past the current cursor position and allows an assertion 
about the contents, but without moving the cursor position.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list