use strict; use warnings; my $str = '../BAC-final/bac_merged1_no9.csv'; my ($substr) = $str =~ m!.*/([^_]*)_!; print "'$substr'"; #### 'bac'