I have a string ’11 15 ‘. W/ a Regex I then compare the values within that string, in this case 11 and 15 (could be any number of digits but I’ll keep it simple with 2 2-digit numbers).
For each of those numbers, I then see if it matches any of the numbers I want; in this case I want to see if the number is ’12’, ’13’, or ’14’. If it is, then I change the value of ‘$m’:
my $string = '11 15 ';
while ( $string =~ /([0-9]{1,})\s+/ig ) {
my $m = $1;
print $m . ".....";
$m = 'change value' if $m =~ /[12...14]{2,}/g;
print $m . "\n";
}
Produces:
11.....change value
15.....15
’15’ stays the same, as it should. But ’11’ changes. What am I doing wrong?
[12...14]matches against “1”, “2”, “.”, and “4”. “11” Matches that; “15” doesn’t. If you’re just matching against numbers, you shouldn’t be using regular expressions. Change your line to the following:Or, if unable to guarantee perl >= v5.10: