in reply to Problem with 2nd string match in file using regex with gm operators

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