473,795 Members | 2,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I have problem with a simple negative lookahead Reqular Expression

The regular expression is /(?!((00000)|(11 111)))/ in oRe. That is
oRE=/(?!((00000)|(11 111)))/
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)|(11 111)))/ returns
-1, 0, 0 respectively - this is correct
The javascript used is Microsoft Javascript 1.5

Jan 18 '07 #1
7 3150

in******@aol.co m wrote:
The regular expression is /(?!((00000)|(11 111)))/ in oRe. That is
oRE=/(?!((00000)|(11 111)))/
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)|(11 111)))/ returns
-1, 0, 0 respectively - this is correct
The javascript used is Microsoft Javascript 1.5
Hi

I think 0, 1, 1 is correct.

Firstly, remember that ?! is non-consuming. I.e. "Lookaheads do not
consume characters, that is, after a match occurs, the search for the
next match begins immediately following the last match, not after the
characters that comprised the lookahead."

Secondly, remember that a look-ahead is testing not for a character,
but what characters follows from a given point in the string.

Conceptually, one way I like to understand a look-ahead is to consider
it as an imaginary "cursor" in the string, looking at what follows that
cursor.

Thus with your RegExp /(?!((00000)|(11 111)))/ and given the string
"0000", I will use ' | ' to refer to the cursor:

1. The cursor starts at the beginning of the input: |0000

This is insertion point ' 0 '

In that case the match fails, so because ?! is non-consuming, the
cursor moves forward one position, and the test is run again:-

2. The cursor is now here: 0|000

This is insertion point ' 1 '

Here only three ' 0 ' characters that follow the cursor, so the match
succeeds.

Hence why it returns 1, because at position 1 there are only 3 zeros in
the look-ahead.

If my string were composed of 5 zeros "00000", then the match would be
at position 2 --- 00|000

Hope this helps

Regards
Julian Turner

Jan 18 '07 #2
Most helpful,
However, in my opinion shows an inconsistency in the search result. It
should return 0 for the string "92708" as it does not find "92708" at
cursor position 0 and the RegExp is a negative lookahead.
Then for "00000" it should return -1 as it does find the string but the
negative should then return -1.

I hope these expectations are OK

Thanks

P.S. Otherwise, I must write a different test depending on the RegExp I
have. Not a nice condition.

Julian Turner wrote:
in******@aol.co m wrote:
The regular expression is /(?!((00000)|(11 111)))/ in oRe. That is
oRE=/(?!((00000)|(11 111)))/
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)|(11 111)))/ returns
-1, 0, 0 respectively - this is correct
The javascript used is Microsoft Javascript 1.5

Hi

I think 0, 1, 1 is correct.

Firstly, remember that ?! is non-consuming. I.e. "Lookaheads do not
consume characters, that is, after a match occurs, the search for the
next match begins immediately following the last match, not after the
characters that comprised the lookahead."

Secondly, remember that a look-ahead is testing not for a character,
but what characters follows from a given point in the string.

Conceptually, one way I like to understand a look-ahead is to consider
it as an imaginary "cursor" in the string, looking at what follows that
cursor.

Thus with your RegExp /(?!((00000)|(11 111)))/ and given the string
"0000", I will use ' | ' to refer to the cursor:

1. The cursor starts at the beginning of the input: |0000

This is insertion point ' 0 '

In that case the match fails, so because ?! is non-consuming, the
cursor moves forward one position, and the test is run again:-

2. The cursor is now here: 0|000

This is insertion point ' 1 '

Here only three ' 0 ' characters that follow the cursor, so the match
succeeds.

Hence why it returns 1, because at position 1 there are only 3 zeros in
the look-ahead.

If my string were composed of 5 zeros "00000", then the match would be
at position 2 --- 00|000

Hope this helps

Regards
Julian Turner
Jan 19 '07 #3

in******@aol.co m wrote:
Most helpful,
However, in my opinion shows an inconsistency in the search result. It
should return 0 for the string "92708" as it does not find "92708" at
cursor position 0 and the RegExp is a negative lookahead.
Then for "00000" it should return -1 as it does find the string but the
negative should then return -1.

I hope these expectations are OK

Thanks

P.S. Otherwise, I must write a different test depending on the RegExp I
have. Not a nice condition.
Hi

Hmm, I still think the results you are getting are correct,
unfortunately for you.

Remember that you have two different tests,

one negative (?!) (which is a very wide set of INFINITY-1 items, i.e.
looking for NOT "0000", means that everything else in the universe
other than "0000" matches).

one positive (?=) (which is a very narrow set of 1 item, i.e.
everything which is "0000")

So with the string "92708"

The positive test (?=) you are looking for "a point followed by 0000"
somewhere in the string. You would expect to produce a failure -1,
because at no point in the string can you say that "0000" follows.
I.e. if I put an imaginary cursor at each point in the string |92708
9|2708 92|708 927|08 9270|8 and look ahead, there is no following
pattern of "0000".

The negative test (?!) you are looing for "a point NOT followed by
0000" . You would expect to produce a successful result ' 0 ', in the
string "92708", because |92708 is the FIRST POINT at which this test
is satisfied and you can say that the point is NOT followed by 0000.

Regards

Julian Turner

Jan 19 '07 #4

[snip]
in******@aol.co m wrote:
The expression used is checkStr.search (oRE). The values returned are
are 0,1,1 - the values should be 0,-1,-1.
[/snip]

Hi again

What exactly are you trying to achieve here? It may be that
look-aheads are not the best answer.

Regards

Julian Turner

Jan 19 '07 #5
This is part of a validation suite where coders may provide a regular
expression; in this case someone coded the negative lookahead to
validate zip codes and it validation suite failed (zip codes 00000 and
11111 are syntactically correct but not valid). The test is done via
the string search method and I expected the same behavior for this
'negative lookahead' RegExp as with other regular expressions.

Very interesting subject; I have not looked at the philosophical side
of the whole thing.

Thanks

Julian Turner wrote:
[snip]
in******@aol.co m wrote:
The expression used is checkStr.search (oRE). The values returned are
are 0,1,1 - the values should be 0,-1,-1.
[/snip]

Hi again

What exactly are you trying to achieve here? It may be that
look-aheads are not the best answer.

Regards

Julian Turner
Jan 19 '07 #6


On Jan 17, 4:17 pm, intra...@aol.co m wrote:
The regular expression is /(?!((00000)|(11 111)))/ in oRe. That is
oRE=/(?!((00000)|(11 111)))/
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 0,1,1 result appears to be correct, as the regular expression is
not anchored.

Perhaps /^(?!((00000)|(1 1111)))/ was intended?

../rh

Jan 19 '07 #7
When I change the RegExp to /^(?!((00000)|(1 1111)))/ I get the intended
results 0,-1-1.

Thanks

ro********@gmai l.com wrote:
On Jan 17, 4:17 pm, intra...@aol.co m wrote:
The regular expression is /(?!((00000)|(11 111)))/ in oRe. That is
oRE=/(?!((00000)|(11 111)))/
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 0,1,1 result appears to be correct, as the regular expression is
not anchored.

Perhaps /^(?!((00000)|(1 1111)))/ was intended?

../rh
Jan 22 '07 #8

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

Similar topics

8
1825
by: gunawardana | last post by:
I have to write a program to verify text field in HTML forms. So,I hane to verify a text field with lenth 10 & maxlenth 10.The entered text should be as follows. xxxxxxxxxy where xxxxxxxxx denotes a combination of numbers and y should be one of 'X','x','V'or 'v'.Also inputs such as 000000000v,000000000X are not possible.
10
1952
by: Michael G | last post by:
double A = floor((2447297.0 - 122.1)/365.25); i get differing results. It varies between 6699 and 6700. The correct answer is 6699. The 6700 result really throws the entire group of calculations off. Any suggestions welcome. Thanks Mike
25
521
by: bruce.james.lee | last post by:
hi i have a problem with integer subtraction in C. printf("%d", c < (a - b)); a is got from a #define and is 0x80000000 and b is got from input and is also 0x80000000. c is ffffffff (-1). Now, this should print 1 (true) but it prints 0! If I modify this to d = c < (a - b);
4
4958
by: August Karlstrom | last post by:
Hi, How come the modulus operator doesn't always yield non-negative values? I expect e.g. (-1) % 3 to be 2 but I get -1. If I want to decrement a cyclic variable n by k I have to write something like n = ((n - k) % length + length) % length where as in e.g. Pascal i can simply write
10
1383
by: John Salerno | last post by:
Hi all. I'm just starting out with Python, so I'm a little slow right now. :) Can someone explain to me why the expression 5 / -2 evaluates to -3, especially considering that -2 * -3 evaluates to 6? I'm sure it has something to do with the negative number and the current way that the / operator is implemented, but why doesn't it evaluate to -2 instead?
8
2216
by: tobiah | last post by:
(?=...) 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...
2
7265
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>
5
2316
by: vbgunz | last post by:
/* * BEGIN EXAMPLES */ var text = 'A Cats Catalog of Cat Catastrophes and Calamities'; /*** * EXAMPLE 1: negative lookahead assertion logic ***/
2
1516
by: Bart Kastermans | last post by:
I have a file in which I am searching for the letter "i" (actually a bit more general than that, arbitrary regular expressions could occur) as long as it does not occur inside an expression that matches \\.+?\b (something started by a backslash and including the word that follows). More concrete example, I have the string "\sin(i)" and I want to match the argument, but not the i in \sin. Can this be achieved by combining the regular...
0
9673
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
9522
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
10443
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
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7543
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
6783
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();...
1
4113
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
3728
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.