Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Re: Re: negative Lookahead

by wog (Curate)
on Nov 12, 2001 at 08:15 UTC ( #124737=note: print w/replies, xml ) Need Help??

in reply to Re: Re: negative Lookahead
in thread negative Lookahead or

What appears to be happening with the second string is this:

You match HIT. Then you go looking matching [^<]*?. The *? first tries to match the minimal number of characters, so it gets 0. And, the negative look-ahead does not try to do backtracking to make that *? match more so it can make the negative look-ahead match. (update: because, if it did that, then the look-ahead would be doing more then just looking ahead in the string.) Thus, it doesn't find a match and the negative lookahead succeeds, so the positive lookahead succeeds.

Replies are listed 'Best First'.
Re: Re: Re: Re: negative Lookahead
by eddmund (Initiate) on Nov 12, 2001 at 21:00 UTC
    OK, thanks. Considering switching to greedy instead of non-greedy results in the regex
    $string =~ / ( HIT (?= [^<]* (?! <a[^>]*> ) .*?<\/a> ) .*?<\/a> ) /x;
    but still gives the same results. If the $string
    '<a href="#1">--HIT<a name="MUST NOT MATCH">--</a> <a href="#2">2</a>'
    does properly not match and
    '<a href="#1">--HIT-<a name="MUST NOT MATCH">--</a> <a href="#2">2</a> +'
    does "falsely" match I don't understand why the problem lies in the fact that a negative lookahead does not backtrack.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://124737]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-12-09 03:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found