my ($name) = $file =~ /([^\\]+?) # everything but backslashes, non-greedy (?:\.[^\\.]*)? $ # optional .ext, anchored at the end /x;