in reply to regex only matching from last match

The match operator with the /g option will match all the patterns if it is used in list context.   However, when used in scalar context it iterates through each pattern in turn, which is what you are experiencing.

  • Comment on Re: regex only matching from last match

Replies are listed 'Best First'.
Re^2: regex only matching from last match
by AnomalousMonk (Archbishop) on Sep 20, 2009 at 02:41 UTC
    Further to jwkrahn's reply, consider:
    >perl -wMstrict -le "my $str = '123 456 789'; print qq{1st match: $1} if $str =~ m{ (\d{3}) }xmsg; print qq{2nd match: $1} if $str =~ m{ (\d{3}) }xmsg; print qq{3rd match: $1} if $str =~ m{ (\d{3}) }xmsg; " 1st match: 123 2nd match: 456 3rd match: 789
    Try those matches without the  //g modifier.
      Thanks AnomalousMonk and jwkrahn, this explained it perfectly.