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

No no no, you're missing my point. I realize that ?? only matches once, but shouldn't the second regex be seperate from the first regex?
/regex1/ /regex2/
Regex1 and 2 shouldn't have anything to do with each other, should they? But in my example, the two ??'s are affecting each other, but they're completely seperate!

Replies are listed 'Best First'.
Re^3: Bizarreness in ?PATTERN? and g
by Mr. Muskrat (Canon) on Jun 04, 2004 at 05:09 UTC

    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.

      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 ]