I'm having a strange issue with some pattern matching I'm doing in a multiline file. I'm using a regular expression with the gm operators to search for two different patterns. When I run my perl script it finds the first pattern but fails on the 2nd even though the pattern is clearly in the file. Here is a test script I wrote that shows the problem.
#!/usr/local/bin/perl use strict; use warnings; my $contents =<<EOT; #################### # GEOGRAPHY CONFIG # #################### environment.type = eu deployment.type = au EOT #--------------------------------------------------------------------- +--------- # Check that the properties deployment.type and environment.type exist #--------------------------------------------------------------------- +--------- if ( $contents !~ /deployment.type/gm ) { print "ERROR: deployment.type not found in contents\n"; } else { print "deployment.type matched\n"; } if ( $contents !~ /environment.type/gm ) { print "ERROR: environment.type not found in contents\n"; } else { print "environment.type matched\n"; }
When I run this perl script I get the following:
C:\test>perl test.pl deployment.type matched ERROR: environment.type not found in contents
One thing I have noticed is if I flip the two pattern searches around (search for environment.type first and deployment.type 2nd, like they exist in $contents) it finds both patterns. But if the pattern searches are reveresed in order from the way they appear in $contents the 2nd pattern match fails.

It's like once it matches the 1st string it gets stuck at the end of $contents and doesn't being searching from the beginning of $contents when looking for the 2nd string search.

I am tyring to understand why this happens? Why should the order of these pattern searches matter? I thought that the gm operators would search all of $contents from begining to end and be reset before beginning the 2nd search but it doesn't seem like that is happening. Does anyone know why this occurs?

In reply to 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.