Positive lookahead assertion

T

tobiah

(?=...)
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?

Thanks,

Toby
 
T

tobiah

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.
 
P

Paddy

tobiah said:
(?=...)
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?

Thanks,

Toby
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'.

Unfortunatley the above may be just as hard to decipher as the original
;-)

- Paddy.
 
N

Neil Cerutti

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. ;)
 
T

tobiah

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')
 
P

Paddy

tobiah said:
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.

- Paddy.
 
C

Carl Banks

tobiah said:
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
 
P

Paddy

Paddy said:
Notice that in the last search below, something else needs to follow,
but is not consumed.


- 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

# The first else after something(4, 5)

# The first else after somethingsomething else(5, 6)

# The first E after S but only if the S was followed immediately by S E
Back to bed for me in the UK.

- Paddy.
 
S

Steve Holden

tobiah said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top