473,387 Members | 1,899 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,387 software developers and data experts.

I have problem with a simple negative lookahead Reqular Expression

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
The javascript used is Microsoft Javascript 1.5

Jan 18 '07 #1
7 3106

in******@aol.com wrote:
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
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)|(11111)))/ 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.com wrote:
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
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)|(11111)))/ 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.com 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.com 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.com 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.com wrote:
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 0,1,1 result appears to be correct, as the regular expression is
not anchored.

Perhaps /^(?!((00000)|(11111)))/ was intended?

../rh

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

Thanks

ro********@gmail.com wrote:
On Jan 17, 4:17 pm, intra...@aol.com wrote:
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 0,1,1 result appears to be correct, as the regular expression is
not anchored.

Perhaps /^(?!((00000)|(11111)))/ 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
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...
10
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...
25
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,...
4
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...
10
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...
8
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...
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: =========================...
5
by: vbgunz | last post by:
/* * BEGIN EXAMPLES */ var text = 'A Cats Catalog of Cat Catastrophes and Calamities'; /*** * EXAMPLE 1: negative lookahead assertion logic ***/
2
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.