use strict; use warnings; my @strs = qw(id_1_1 id_2 banana id_1_2_); for my $id (@strs) { if ($id =~ /(\d)(?!_)/) { print "Extracted $1 from $id\n"; } else { print "no match for $id\n"; } }