Since you haven't used the /m modifier, Perl won't treat it as a "single line".

No. That's backwards. Single-line is the default. m switches to (m)ultiline mode.

Furthermore, m only affects the defintion of ^ and $. Since neither are used here, whether m is present or absent is irrelevant.

The replacement first takes off the first three characters (which includes the newline), and then it goes for another pass to get the last two characters.

No. In fact, the \n is a red herring. The same problem occurs with my $string = "1234";.

The first pass sees the characters at pos 0 to 3 replaced with "go", setting pos = 4.
The second pass sees the characters at pos 4 to 4 replaced with "go", setting pos = 4.
The third pass ends the g loop since the only possible match would start and end at the same positions as the second pass.

my $string = "1234"; $string =~ s/.*/print("($&)");'go'/egs; # (1234)()

What I find most interesting, however is that the /g seems to have something to do with it too. That is /ms does one replacement

With and without g, both regex do the same first substitution. With g, it proceeds to do other possible substitutions. That's the very definition of g.


In reply to Re^2: Regex Pop Quiz with .*, /g, and /s by ikegami
in thread Regex Pop Quiz with .*, /g, and /s by saintmike

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.