in reply to String Matching

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re^2: String Matching
by frozenwithjoy (Priest) on Aug 14, 2012 at 16:49 UTC
    Just for the record, the standard quantifiers in Perl regular expressions are, indeed, greedy.

      They are greedy, but not too clever

      my $str = "xaxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; $str =~ /(a+)/; print "$1\n"; # prints "a" not "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      The quantifiers are greedy, but they refuse to let go of something they found unless forced even if they could get more someplace later.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

        I'll see you and raise you.   /(a*)/ (note * quantifier in place of +) matches, but captures nothing, not even a single 'a'.

        >perl -wMstrict -le "my $s = 'xaxaaaaa'; print qq{matched, captured '$1'} if $s =~ /(a*)/; " matched, captured ''
        Um, they start from the left, stop when they find a match -- that is clever