Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

(dooberwa) I hold with beliefs that your regex is incorrect

by dooberwah (Pilgrim)
on Feb 22, 2002 at 22:19 UTC ( [id://146982]=note: print w/replies, xml ) Need Help??


in reply to Re: (dooberwah) I'm don't think that gives the same results
in thread Regular Expression Tweaking

Instead of just trusting the documentation I went and did my own tests on differences in regex assertions. Here are my results:

dooberwah@kyle:~$ perl -e 'print "foobar" =~ /(?<=foo)bar/, "\n";' 1 dooberwah@kyle:~$ perl -e 'print "foobar" =~ /(?=foo)bar/, "\n";' dooberwah@kyle:~$

As you can see, trying to use a look-ahead assertion in place of a look-behind assertion yealds no results. I certainly hope that we're still talking about the same thing. You got me a little confused with the jumping analogy :-). If your actually talking about something different please just tell me.

-Ben Jacobs (dooberwah)
http://dooberwah.perlmonk.org
"one thing i can tell you is you got to be free"

Replies are listed 'Best First'.
Re: (dooberwa) I hold with beliefs that your regex is incorrect
by Anonymous Monk on Feb 23, 2002 at 13:55 UTC
    Now you've really confused yourself. :)

    Your tests are irrelevant in this case. I was using a zero-width assertion. "foo" is not zero-width.

    Just consider the second regex. That cannot be true, no matter what string you try it on. Because (?=foo) looks if the next three chars are "foo", but then you at the same time try to match "bar". So that will never be true.

    What a look-behind does is just to set how many steps behind current position it should look. In your case it's three because "foo" is three bytes long. You might be familiar with that look-behinds cannot be variable length. This is closely related to that implementation. Besides that there's no fundamental difference between look-behinds and look-aheads. A look-ahead just steps zero steps back and hence start looking at the current position. The limitation of only being able to use a constant-width patterns for look-behinds is checked at regex compile-time, and at regex run-time the regex engine uses the same method for both types of assertions. Theoretically the look-ahead takes X steps back, it's just that X is always 0.

    If the look-behind pattern then is zero-width, the engine takes zero steps back to start it's matching, and thus effectively is the same as a look-ahead.

    Cheers,
    -Anomo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-18 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found