in reply to Do nothing? or Do something important in a very obscure way?

Well, let's look at it from the regex engine's point of view, executing 'xfoo' =~ m/x??:foo/;. First, it tries to match x zero times (since x? can match zero times, and x?? tells it to be non-greedy about it, it starts with zero, and backtraces up from there). That succeeds. Then, it tries to match foo, fails, and backtraces. But ohno, it can't backtrace. But what it can do is try matching the whole regex a little later (I'm not sure; does this count as backtracing past the ":"?)


Confession: It does an Immortal Body good.

Replies are listed 'Best First'.
Re: Re: Do nothing? or Do something important in a very obscure way?
by BrowserUk (Patriarch) on Aug 23, 2002 at 08:50 UTC

    I'm genuinely not sure if you answered my dilemma or not:)

    Does "it tries to match x zero times" mean, it just moves on to the next part of the regex and doesn't bother looking at the string under inspection, effectively completely ignoring the presence of 'x??' or 'x*?'? Or does it look to make sure that there isn't an 'x' in the string between the current point and the next significant point?


    What's this about a "crooked mitre"? I'm good at woodwork!
      The RE engine is a recursive search for ways to try to match the pattern to the string.

      'x??' means that it notes that it has a decision, and first tries to match no x's. If it fails then it will backtrack, reconsider that decision, and try to match one x then go forward. If that fails, then you fail.

      'x*?' is similar. It notes that it has a decision, and first tries to match no x's. If it fails then it will backtrack, try to match one x, and then try to make the same decision over again. (That is the difference between the * and the ?. The ? will try to match at most once then proceed. The * returns to trying to match the preceeding group, and therefore can match there an unlimited number of times.)

      If that didn't make sense then pick up Mastering Regular Expressions by Friedl. He explains it better.

        Please see my update to the post regarding context!


        What's this about a "crooked mitre"? I'm good at woodwork!

      Yes. <g>

      By "it tries to match x zero times", I meant "it ignores the atom". I should point out that I'm hardly an expert.


      Confession: It does an Immortal Body good.