473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Positive lookahead assertion

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

--
Posted via a free Usenet account from http://www.teranews.com

Sep 7 '06 #1
8 2203
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.

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?

Thanks,

Toby
--
Posted via a free Usenet account from http://www.teranews.com

Sep 7 '06 #2

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?

Thanks,

Toby

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

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

- Paddy.

Sep 7 '06 #3
On 2006-09-07, Paddy <pa*******@netscape.netwrote:
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
Sep 7 '06 #4
>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')

--
Posted via a free Usenet account from http://www.teranews.com

Sep 7 '06 #5

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.

Sep 8 '06 #6
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

Sep 8 '06 #7
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.

Sep 8 '06 #8
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

Sep 8 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Todd Miller | last post by:
Hi, I recently discovered an assertion failure in the Python garbage collection system when scripts using our C extension (numarray) exit. The assertion is activated for Pythons configured using...
0
by: benevilent | last post by:
Hey, I'm getting an assertion error as a result of embedding python. "Modules/gcmodule.c:231: visit_decref: Assertion `gc->gc.gc_refs != 0' failed." I only get this assertion error with...
4
by: Morgan Leppink | last post by:
Hey all - We are running SQL 2000 with ALL available service packs, etc. applied. We just built a brand new database server, which has dual 2Ghz XEONs, 2GB memory, and the following disk...
1
by: Timur Safin | last post by:
Hi All, Sorry if it is offtopic here, I wasn't able to find any more relevant group... I'm slowly approaching AMD64 build for our product (as my personal fun project). And after I ran that...
5
by: Ron Louzon | last post by:
I have some C++ code that uses the CSingleLock( CCriticalSection *) constructor. In visual C++ 6.0, this code compiles and runs fine in both Debug and release modes. However, in Visual Studio...
4
by: Mullai | last post by:
Hi , My program gives an error message like this Debug Assertion Failed! program:................ File: wincore.cpp Line: 958 Please can anyone help me out in this issue. I have to solve...
2
by: writebrent | last post by:
I think I need to do a negative lookahead with a regular expression, but I'm a bit confused how to make it all work. Take these example texts: Need to match these two: =========================...
7
by: intrader | last post by:
The regular expression is /(?!((00000)|(11111)))/ in oRe. That is oRE=/(?!((00000)|(11111)))/ The test strings are 92708, 00000, 11111 in checkStr The expression used is checkStr.search(oRE). The...
5
by: vbgunz | last post by:
/* * BEGIN EXAMPLES */ var text = 'A Cats Catalog of Cat Catastrophes and Calamities'; /*** * EXAMPLE 1: negative lookahead assertion logic ***/
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.