Strange issue with regex matching in perl, alternate attempts match
perl, regex
Solution
Get rid of `m` and `g` as modifiers to your regex, they aren't doing what you want.
print "1. matched using regex\n" if ($str =~ /total-found=(\d+)/);
print "2. matched using regex\n" if ($str =~ /total-found=(\d+)/);
print "3. matched using regex\n" if ($str =~ /total-found=(\d+)/);
print "4. matched using regex\n" if ($str =~ /total-found=(\d+)/);
Specifically, the `m` is optional in this context `m/foo/` is exactly the same as `/foo/`. The real problem is that `g` does a bunch of things you don't want in this context. See perlretut for details.
Problem
Consider the following perl script: ``` #!/usr/bin/perl my $str = 'not-found=1,total-found=63,ignored=2'; print "1. matched using regex\n" if ($str =~ m/total-found=(\d+)/g); print "2. matched using regex\n" if ($str =~ m/total-found=(\d+)/g); print "3. matched using regex\n" if ($str =~ m/total-found=(\d+)/g); print "4. matched using regex\n" if ($str =~ m/total-found=(\d+)/g); print "Bye!\n"; ``` The output after running this is: ``` 1. matched using regex 3. matched using regex Bye! ``` The same regex matches once and does not match immediately after. Any idea why the alternate attempts to match the same string with the same regex fail in perl? Thanks!