in reply to Why do zero width assertions care about lookahead/behind?

If you think a bit about what variable length look behind means for backtracking, you may realize why it's not in Perl right now.

If you really want variable length look behind and only need fixed length look ahead, reverse your string and adapt your regex accordingly.

Liz

  • Comment on Re: Why do zero width assertions care about lookahead/behind?

Replies are listed 'Best First'.
Re: Re: Why do zero width assertions care about lookahead/behind?
by tilly (Archbishop) on Oct 09, 2003 at 05:34 UTC
    OK, I thought about what variable-length look behind means for backtracking. The answer is that if your regular expression engine has a mode where it scans through the string backwards that it goes into at the right time, then variable-length look behind is the same impact as variable-length look ahead.

    Yes, implementing this is a pain. I am not willing to implement it. So I am not willing to complain that it doesn't exist in Perl.

    However I will admit to being irritated that Java's RE engine supports variable length look behind while Perl's doesn't.

      ...has a mode where it scans through the string backwards...

      The way I see it, is that the whole regular expression engine should be able to go either forward or backward. At any point of its execution. To be really able to have variable length look-behind.

      (?<=foo.*?) # if there is a "foo" before the match
      is but the beginning. How about:
      <?<=foo.*?(?=.*?bar)) # if there is a "foo" before, with a "bar" afte +r it

      To do this properly, you would need to rewrite the whole regex engine, I'm afraid (and what I remember from one of MJD's talks about how he hacked the regular expression engine). This may/will happen for Perl6. So I guess we'll have to hold our breath until then.

      Meanwhile, I wonder whether you wouldn't be able to hack something with (?{code}) and/or (??{code}), grabbing the string before the match so far, reversing it and feeding that a reversed regex. Possibly in combination with a source filter.

      Hmmm... maybe I shouldn't have these evil thoughts this early in the morning. ;-)

      Liz

        You're right. You shouldn't have evil thoughts like this. And I shouldn't encourage them.

        So you didn't, I won't, and sanity won't be eroded a little more. :->