Extract text from a multiline string using Perl

前端 未结 3 1830
太阳男子
太阳男子 2020-12-31 14:41

I have a string that covers several lines. I need to extract the text between two strings. For example:

Start Here Some example
text covering a few
lines. En         


        
3条回答
  •  感动是毒
    2020-12-31 15:17

    Use the /s regex modifier to treat the string as a single line:

    /s Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.

      $string =~ /(Start Here.*)End Here/s;
      print $1;
    

    This will capture up to the last End Here, in case it appears more than once in your text.

    If this is not what you want, then you can use:

      $string =~ /(Start Here.*?)End Here/s;
      print $1;
    

    This will stop matching at the very first occurrence of End Here.

提交回复
热议问题