Let's elaborate. <FILE> in scalar context will only read one line. By default, that means it will only read until (and including) the next \n. How can a line match \w+\n\n\w+ if a line can't contain \n other than at the end?
The fix would be to read the whole file in at once, as follows:
my $text; { open(my $test_fh, '<', 'testfile) or die "Unable to open testfile: $!\n"; local $/; # Read to end of file. $text = <$test_fh>; } if ($text =~ /\w+\n\n\w+/) { print "reading file test:\n$text\nmatches.\n"; }
Note: The m modifier on your regexp is useless since you don't use ^ or $. The s modifier on your regexp is useless since you don't use ..
Update: If you want to find all matches, use the following:
... while ($text =~ /\w+\n\n\w+/g) { print "reading file test:\n$text\nmatches.\n"; }
In reply to Re^2: multiline regex: heredoc vs. reading file
by ikegami
in thread multiline regex: heredoc vs. reading file
by bowei_99
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |