Positive lookahead assertion

Neil Cerutti horpner at yahoo.com
Thu Sep 7 16:32:34 EDT 2006


On 2006-09-07, Paddy <paddy3118 at netscape.net> wrote:
> tobiah wrote:
>> (?=...)
>> Positive lookahead assertion. This succeeds if the contained
>> regular expression, represented here by ..., successfully
>> matches at the current location, and fails otherwise. But,
>> once the contained expression has been tried, the matching
>> engine doesn't advance at all; the rest of the pattern is
>> tried right where the assertion started.
>>
>> I am unable to wrap my mind around this sentence.  Could
>> someone give me an example of how this works, and why it would
>> be useful?
>
> 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'.

At any rate it further blurs the line between parsing and
pattern-matching. ;)

-- 
Neil Cerutti



More information about the Python-list mailing list