use strict; use warnings; my @array = qw(id_1_1 id_2); for (@array){ print "$1\n" if (m/id\_(\d+)(?!\_)/); } __END__ Output: ------ 2