user3635458 user3635458 - 1 year ago 62
Perl Question

Perl regex match string including newline

How do I match something like this:


I want to extract between
including the newline character, I was using the regex below but failed:


What I expected to be extracted:


I tried with the other modifiers like
, but it doesn't work.

Answer Source

You need (?s) modifier

$x =~ s/(?s)haha\s*(.*?)\s*lolo/$1/;
print $x;

or lookahad and lookbehind

$x =~ m/(?s)(?<=haha)\s*(.*?)(?=\s*lolo)/;
print $1;

NOTE :- (?s) is DOTALL modifier. It allows to matche newline with .