I’m trying to basically trying to separate a specific amount of text from the one or more numbers that appear at the end. The below works when there is 1 trailing number but not when there is two or more? Shouldn’t the (\d+) be getting the “12” in “P_TIME12”?
my @strs = ('P_ABC1','P_DFRES3','P_TIME12');
foreach my $str (@strs) {
if ($str =~ /^P_(\w+)(\d+)$/) {
print "word " . $1 . " digits " . $2 . "\n";
}
}
Results in
word ABC digits 1
word DFRES digits 3
word TIME1 digits 2
TIA
\w matches “word characters”, including digits and underscore. Because you’ve asked for at least one digit (\d+), \w is being greedy and matching one as well.
You should be more explicit than
\w, and use/^P_([A-Za-z_]+)(\d+)$/instead.