473,802 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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|ast rophes))/g;
alert(text.repl ace(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 2317
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.de mon.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.de mon.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(?!\.7 89)) -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(?!\.7 89)) -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(?!\.7 89)) -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
6152
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 non-greedy wildcard (*) in the Perl regular expression engine? The reason I ask is the following scenario: I am try to match a pattern B as long as it is not preceded by a pattern A in a given string, regardless of what occurs between B and
7
8227
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 negative numbers being hidden from me :( I tried checking Tools/Options/Connections/Use Regional Settings both on and off, stopping and restarting M$ Query Analyer in betwixt, but no improvement.
12
28975
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 If rst!Amount < 0 Then rst!Amount = rst!Amount - (rst!Amount * 2)
3
3180
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 1000). When I have a lookup table w/ the "threshold" amounts of income, I'm not able to get dlookup to work, and I'm pretty sure it's getting tripped up by negative / positive lookup values. Is it not possible to do lookups on a table w/ both...
19
2436
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 internally for storing the negative numbers . why it uses 2's compliment but not the ones compliment for storing the negative numbers ?
8
2217
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>
7
3151
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
0
1450
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, -12x + 60 = -48 in my project i have 6 textboxes for the numbers and two of them are for "X" if the problem has the "X" after the 10 anyway in this project i show the steps for figuring thes problems out right down to the answer.(with labels...
0
9699
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
10536
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...
1
10285
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
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...
0
9114
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
7598
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
6838
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
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4270
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

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.