I think I’m missing something simple here…
$key = "deco-1-LB-700F:MAR:40";
if ($key =~ m/deco-(.*?)-(.*?)-(.*?):(.*?):(.*?)/) {
print "1=$1 2=$2 3=$3 4=$4 5=$5";
}
This results in the output: 1=1 2=LB 3=700F 4=MAR 5=
Why isn’t $5 returning the value 40 ?
Cheers, Stu
Because
.*?is lazy and will match zero characters if it can. Anchor the regex to the end of the string:But it’s nearly always better to use something more explicit than the catch-all
.*and.*?. Tell the regex engine exactly what you want to allow to match. Assuming that the delimiters-and:never occur in the actual matches, I suggest[^-]means “match any character except-“.[^:]means “match any character except:“.