my $text; { local $/ = undef; $text = ; #"slurp" the whole file } while ($text =~ / (\d+) \. pace (.*?) \d+ \. slow /xmsg ) { print "filename=$1 and text=$2"; } __DATA__ 12345.pace text1 12345.slow 123456.pace text2 123456.slow hyds.pace bla bla bla uiw.slow --output:-- filename=12345 and text= text1 filename=123456 and text= text2