use strict; use warnings; my $regexp = qr{ Temp: # hit => the temp: keyword (.+?) # capture what follows (?: # until (?=[\s-]+Temp) # another temp: shows up | # or \s*\z # the end of the data is here ) }msx; open my $fh, '<', 'tmpvar.dat' or die "wtf, $!"; { local $/; # slurp the entire file into my $content = <$fh>; # a buffer while($content =~ /$regexp/g) { # and extract each temp: record (my $var_temp = $1) =~ tr/\r\n//d; # delete new line (if any) print "var_temp=$var_temp\n" # and print } } close $fh;