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

positive/negative lookahead issue. greedy = problems?

/*
* BEGIN EXAMPLES
*/

var text = 'A Cats Catalog of Cat Catastrophes and Calamities';

/***
* EXAMPLE 1: negative lookahead assertion logic
***/

var newString = text.split(/\s/);
for (var i in newString) {
var word = newString[i];
if (
// we'll replace the word Cat under these conditions:
word.search(/Cat/) == 0 && // *if* word begins with Cat
word != 'Catalog' && // *but* is not Catalog
word != 'Catastrophes' // *and* is not Catastrophes
) {
// we'll replace the word Cat with Human
newString[i] = (word.replace('Cat', 'Human'));
}
}
alert(newString.join(' '));
// -A Humans Catalog of Human Catastrophes and Calamities
/***
* EXAMPLE 2: the simpler version
***/
var pattern = /(Cat(?!alog|astrophes))/g;
alert(text.replace(pattern, 'Human'));
// -A Humans Catalog of Human Catastrophes and Calamities

/*
* END EXAMPLES
*/
example 1 === example 2. it may appear from this point forward it'll
be safe to assume my understanding of a negative look ahead may be
correct. problem is, I feel my understanding may be flawed. here is
why...

I always knew about positive/negative look aheads but didn't truly
understand them. Wrox Professional JavaScript sort of cleared it up
for me *but* upon deciding to sharpen my skills on my own, I came
across a peculiar problem.

Does being greedy work with them?

e.g., I created this problem entirely on my own. I didn't mean to, it
just ended up that way. my idea was to match \d+\.\d+ but not if it
was followed by \.\d+ .

here's an example:

var nla = /(\d+\.\d+(?!\.\d))/; // my negative look ahead
var txt = 'euphoria 72.21.330';
alert(nla.exec(txt)[1]); // -72.2 (completely unexpected)

what did I expect? nothing. null, nada. I am not at all interested in
the dozens of other possible solutions. I am most interested in
understanding this problem. I need enlightenment and very much
appreciate any insight on it!
Nov 24 '07 #1
5 2283
vbgunz wrote:
var nla = /(\d+\.\d+(?!\.\d))/; // my negative look ahead
var txt = 'euphoria 72.21.330';
alert(nla.exec(txt)[1]); // -72.2 (completely unexpected)

what did I expect? nothing. null, nada. I am not at all interested in
the dozens of other possible solutions. I am most interested in
understanding this problem. I need enlightenment and very much
appreciate any insight on it!
Subexpression | Match | Not matched
----------------+-------+------------
\d+ | 72 | .21.330
\d+. | 72. | 21.330
\d+.\d+ | 72.21 | .330
\d+.\d+(?!\.\d) | 72.2 | 1.330
HTH

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Nov 24 '07 #2
vbgunz wrote:
var nla = /(\d+\.\d+(?!\.\d))/; // my negative look ahead
var txt = 'euphoria 72.21.330';
alert(nla.exec(txt)[1]); // -72.2 (completely unexpected)

what did I expect? nothing. null, nada. I am not at all interested in
the dozens of other possible solutions. I am most interested in
understanding this problem. I need enlightenment and very much
appreciate any insight on it!
Subexpression | Match | Not matched
-----------------+-------+------------
\d+ | 72 | .21.330
\d+\. | 72. | 21.330
\d+\.\d+ | 72.21 | .330
\d+\.\d+(?!\.\d) | 72.2 | 1.330
HTH

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Nov 24 '07 #3
Thomas 'PointedEars' Lahn wrote:
Subexpression | Match | Not matched
-----------------+-------+------------
\d+ | 72 | .21.330
\d+\. | 72. | 21.330
\d+\.\d+ | 72.21 | .330
\d+\.\d+(?!\.\d) | 72.2 | 1.330
it makes no sense to me. i read up a bit more on positive/negative
lookaheads and my issue still makes no sense to me. please, read this.

for text replacement operations I find these lookaheads invaluable. it
is awesome to know I can check if a pattern is either followed or not
followed by another pattern (without consuming it). seriously awesome.
what I do not understand is what is happening in my issue. I know you
posted a table to help but the table makes no sense to me at all when
it comes to *why* anything returns :(

lines 1 through 3 make perfect sense. 4 throws my baby out of the
window with the bathwater. I am thinking no match should be made.
seriously. no match should be made. I cannot understand why any match
is made.

(123\.456(?!\.789)) -no match on 123.456.789 -PERFECT!
(\d+\.\d+(?!\.\d+)) -2 matches on 123.456.789 -123.45/6.789 -A
PREFECT WTF!?

I think this is why I never really used lookaheads. I thought I
understood them as of yesterday and using primitive examples they're
very useful *but* when I came up with the problem above, I got thrown
into a world of hurt. I have no idea why a match is made. no match
should be made. so. why is there a match being made?

In the end i would see even 456.789 making sense if it was the only
thing to return. I am just completely thrown off here. any detailed
enlightenment would be very much appreciated!
Nov 25 '07 #4
pr
vbgunz wrote:
(123\.456(?!\.789)) -no match on 123.456.789 -PERFECT!
(\d+\.\d+(?!\.\d+)) -2 matches on 123.456.789 -123.45/6.789 -A
PREFECT WTF!?
It looks like you're thinking of a lookahead as "x followed by y", and
might find it makes more sense to understand it as "x *immediately*
followed by y".

'123.45' is indeed the longest match for digit(s) dot digit(s) not
immediately followed by dot digit(s). '123.45' is immediately followed
instead by '6'.

If you want digit(s) dot digit(s) not followed *anywhere* by dot
digit(s), you're looking at something like:

/(\d+\.\d+(?!.*\.\d+))/

Note the '.*' in the lookahead. That will match '456.789'.

This scenario, from the perspective of Perl regular expressions, is well
described at

http://perldoc.perl.org/perlre.html#Backtracking
Nov 25 '07 #5
pr wrote:
vbgunz wrote:
(123\.456(?!\.789)) -no match on 123.456.789 -PERFECT!
(\d+\.\d+(?!\.\d+)) -2 matches on 123.456.789 -123.45/6.789 -A
PREFECT WTF!?
It looks like you're thinking of a lookahead as "x followed by y", and
might find it makes more sense to understand it as "x *immediately*
followed by y".

'123.45' is indeed the longest match for digit(s) dot digit(s) not
immediately followed by dot digit(s). '123.45' is immediately followed
instead by '6'.

If you want digit(s) dot digit(s) not followed *anywhere* by dot
digit(s), you're looking at something like:

/(\d+\.\d+(?!.*\.\d+))/

Note the '.*' in the lookahead. That will match '456.789'.

This scenario, from the perspective of Perl regular expressions, is well
described at

http://perldoc.perl.org/perlre.html#Backtracking
I'd like to thank you both (PointedEars) for your trying to help.
although it makes a little more sense than it did when I first
encountered the problem, the perldoc resource looks like a great
opportunity to clear matters up even more. every link to one of my
questions is like a gift. clicking one is equivalent to the act of
unwrapping one. I thank you both again for your time :)
Nov 28 '07 #6

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

Similar topics

1
by: Thomas F. O'Connell | last post by:
I've been looking through the negative lookbehind posts and haven't yet found a definitive answer to the question I'm about to ask: Does negative lookbehind have lower precedence than even a...
7
by: pj | last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter whether they are truly positive or negative ? I am having to cast each column to varchar to find out if there are any...
12
by: deko | last post by:
Is there an easy way to make all negative values positive in a particular table? I've been experimenting with this: Dim rst As DAO.Recordset Set rst = db.OpenRecordset("tblNegValues") Do...
3
by: Tripp Knightly | last post by:
I have a lookup table from which I want to categorize various bands of customer net income. Some of the income is positive, some is negative. The bands vary in size (ie, <500, -200 to 0, 100 to...
19
by: Harshan | last post by:
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767) Why the one less at positive Range ? (compared to the negative side..!) I came to know that it uses 2's compliment...
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: =========================...
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...
0
lee123
by: lee123 | last post by:
i am making a program that is about algebra and i want to know how can i get it to put negative and positive numbers in a text box for example if i had a problem that is: -6(2x + 10) = -48, ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
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
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.