in reply to Re^2: simple pattern match ?
in thread simple pattern match ?

On Perl 5.8.5, the pattern works for me:

#!perl -w for (qw( 00 01 02 03 04 05 06 07 08 09 0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 58 59 )) { die "Ooops: $_ didn't match the regular expression" unless /([012345]?\d)/; }; print "All numbers passed.";

Replies are listed 'Best First'.
Re^4: simple pattern match ?
by McDarren (Abbot) on Mar 15, 2006 at 17:12 UTC
    So it does :)

    So my logic above is flawed. I'm almost certain that I tested your pattern earlier and it failed - but obviously not.

    Anyway, I guess that means that the regex engine is "smart enough" to know that even if it gets a match with the [012345]? - it has to "release" it to the \d if there is no 2nd digit - yes?

      Yes. The regex engine uses backtracking and marks its location whenever it has to make a decision. If the first decision doesn't work out, it backtracks to the last marked point and uses the next possible decision. The ? regex operator is such a decision point and I guess that basically, the two possibilities for a match are:

      [012345]\d
      and

      \d
      So in this case, it would not get a match when trying /[012345]\d/ against 5, so it backtracks, and tries /\d/, which works.