for my $entry (qw(1_0 1_0_1 1_0_beta 1_0_1_beta)) { print "$entry\n" if $entry =~ /^\d+ _\d+ (?> (?:_\d+)? \w+) $/x; } #### 1_0_1 1_0_beta 1_0_1_beta