if ($filename =~ /^([^_]+)_.*v(\d+)/) { # do something with $1 and $2 here }