Does anyone know why this occurs?

(in addition to ikegami's response above) - that's also because your $contents

my $contents =<<EOT; #################### # GEOGRAPHY CONFIG # #################### environment.type = eu deployment.type = au EOT

contains " = au\n\n" after deployment.type. The regular expression engine sets its pointer just after the "type" in "deployment.type = au" - but the string doesn't finish at that point. That's why the position isn't reset.

But even if you'd correct that condition, the position at the end of the string would not be reset, since it is only reset if /g is exhausted, passing the end of the string. Consider:

use strict; use warnings; my $contents = " #################### # GEOGRAPHY CONFIG # #################### environment.type = eu deployment.type = au"; if ( $contents !~ /deployment.type = (\w+)/gm) { print "ERROR: deployment.type not found in contents\n"; } else { print "deployment.type matched\n"; } $contents =~ /./g; # <-- this search passes end of string, and resets if ( $contents !~ /environment.type = (\w+)/gm ) { print "ERROR: environment.type not found in contents\n"; } else { print "environment.type matched\n"; } __END__ qwurx [shmem] 01:36 ~ > perl au.pl deployment.type matched environment.type matched

In reply to Re: Problem with 2nd string match in file using regex with gm operators by shmem
in thread Problem with 2nd string match in file using regex with gm operators by knudsj01

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.