my $testname = " 456.txt"; my $filename; if ($filename = $testname=~ /^\s*?(\d+\.txt)\s*?$/ ) { print "Found a file named $filename.\n"; }