#! perl use strict; use warnings; my $s = 'XYX_WT_7_25h_Trimmed_75.SAM'; print $1 if $s =~ / ( _ [^_]+ _ [^_]+ ) $ /x;