for($scalar){ /((?:\/\w+)+\/file\d+.ext)/; $pathtofile=$1; }