Positive lookahead assertion

Paddy paddy3118 at netscape.net
Thu Sep 7 23:15:28 EDT 2006


Paddy wrote:
> 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')
> >
>
> Notice that in the last search below, something else needs to follow,
> but is not consumed.
>
> >>> import re
> >>> re.search(r'something', ' somethingsomethingsomething else').span()
> (1, 10)
> >>> re.search(r'somethingsomething else', ' somethingsomethingsomething else').span()
> (10, 33)
> >>> re.search(r'something(something else)', ' somethingsomethingsomething else').span()
> (10, 33)
> >>> re.search(r'something(?=something else)', ' somethingsomethingsomething else').span()
> (10, 19)
> >>>
>
> - Paddy.

Heres a more complicated example to show its effect on subsequent group
matches.
The lines are getting a little long so if you see S think something; E
think else.

Remember that .*? matches the LEAST amount of following characters, and
that  (?P<name>...) creates a group that can be later referred to by
name.

>>> import re

# The first else after something
>>> re.search(r'S.*?(?P<else>E)',      " SS EE").span("else")
(4, 5)

# The first else after somethingsomething else
>>> re.search(r'S(S E).*?(?P<else>E)',  " SS EE").span("else")
(5, 6)

# The first E after S but only if the S was followed immediately by S E
>>> re.search(r'S(?=S E).*?(?P<else>E)'," SS EE").span("else")
(4, 5)
>>> 

Back to bed for me in the UK.

- Paddy.




More information about the Python-list mailing list