473,804 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2217
Sorry, I should have tried harder. I see that the text
of the match is simply not consumed, so that:

m = re.search('(?=f oo)fo', 'food')

succeeds, while

m = re.search('(?=f ox)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*******@nets cape.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('some thingsomething 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('some thingsomething else')
Notice that in the last search below, something else needs to follow,
but is not consumed.
>>import re
re.search(r's omething', ' somethingsometh ingsomething else').span()
(1, 10)
>>re.search(r's omethingsomethi ng else', ' somethingsometh ingsomething else').span()
(10, 33)
>>re.search(r's omething(someth ing else)', ' somethingsometh ingsomething else').span()
(10, 33)
>>re.search(r's omething(?=some thing else)', ' somethingsometh ingsomething 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('(?=f oo)fo', 'food')

succeeds, while

m = re.search('(?=f ox)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)=="fo o" (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('some thingsomething else')

Notice that in the last search below, something else needs to follow,
but is not consumed.
>import re
re.search(r'so mething', ' somethingsometh ingsomething else').span()
(1, 10)
>re.search(r'so methingsomethin g else', ' somethingsometh ingsomething else').span()
(10, 33)
>re.search(r'so mething(somethi ng else)', ' somethingsometh ingsomething else').span()
(10, 33)
>re.search(r'so mething(?=somet hing else)', ' somethingsometh ingsomething 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 somethingsometh ing 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('some thingsomething 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
3169
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 --with-pydebug. I have a feeling I may be doing something wrong with garbage collection support for some of our c types, but I'm not sure exactly what. Here is the assertion output:
0
1707
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 Python compiled with debugging flags, rathen than the standard library which comes with Debian.
4
6404
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 configuration: RAID 1 array (2 disks) Operating System Windows Server 2003 RAID 1 array (2 disks) Database Logs RAID 10 array (4 disks) Database Data
1
1906
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 Amd64 cross-compiler from DDK 3790 build (proven to be good enough for my driver stuff) over the sources I've updated shortly I've found this serious assertion appearing in manyplaces of the code we have: $ tcc.ver edzunet build5105
5
5556
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 .Net, when I run this code I get an Assertion failure. The error appears to be exactly the same as that seen with CSingleLock in VC++ version 4.0. I can get around this by using the Lock method of the CCriticalSection object but the accepted way...
4
9425
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 this bug immediately in my project.
2
7267
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: ========================= Item 4.01 Regulation and other items <b>Item 4. Regulation</b>
7
3152
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 values returned are are 0,1,1 - the values should be 0,-1,-1. The positive lookahead expressiono RE=/(?=((00000)|(11111)))/ returns -1, 0, 0 respectively - this is correct
5
2319
by: vbgunz | last post by:
/* * BEGIN EXAMPLES */ var text = 'A Cats Catalog of Cat Catastrophes and Calamities'; /*** * EXAMPLE 1: negative lookahead assertion logic ***/
0
9708
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9587
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7623
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.