in reply to Re^2: Bizarreness in ?PATTERN? and g
in thread Bizarreness in ?PATTERN? and g

If you want them separate then remove the g modifier.

$_="a1 a2 a3"; while(?a(\d)?){ print $1; } while(?a(\d)?){ print $1; } __END__ 11

Updated to add in the snippet.

Replies are listed 'Best First'.
Re^4: Bizarreness in ?PATTERN? and g
by BUU (Prior) on Jun 04, 2004 at 05:16 UTC
    I realize that, but why is one affecting the other?

      That is how the g modifier works in scalar context. The match doesn't fail in the first or second attempt nor do you use reset so pos() is not reset and the next pattern will match where the last one left off. Read the entire section of perlop that deals with m/PATTERN/cgimosx & /PATTERN/cgimosx because it explains it very clearly.

      The intent of the interaction between /g and pos() is to allow you to build parsers that "consume" a string. You read the first part, and if that matches, you read the second part, and if that fails, you try an alternative for the second part, and so on.

      Check out the Mastering Regular Expressions book for some ideas on how to keep this in mind, and take advantage of it for parsing tasks.

      --
      [ e d @ h a l l e y . c c ]