The problem is not the match or the regex, I think, but how you are opening the file. The context for
if is scalar, so it just takes just the first, single line of the file and it tests
that against your regex. Try it this way instead:
Code:
perl -e 'print "$1\n" if "This is my\nmulti-line string" =~ /.*(This.*)/s'
The match operator tests against a scalar - ie, it takes one item and tests that against your regex - for example, if you open the file in a
while loop, then perl treats each line, one at a time, as $_ and tests $_ against the regex. To test the whole file
at once you would have to first slurp the whole file into a scalar.
Code:
#!/usr/bin/perl
use strict;
use warnings;
open(my $file, "<", "file")
or die "Can't open file: $!";
my $text = do { local $/; <$file> };
if ($text =~ /.*(This.*)/s) {
print $1;
}
Edit: see this from Perldoc's FAQ
http://perldoc.perl.org/5.8.8/perlfa...all-at-once%3f