Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract text from a multiline string using Perl

Tags:

string

regex

perl

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. End Here

I need to extract the string, Start Here Some example text covering a few lines.

How do I go about this?

like image 674
Lazloman Avatar asked Jul 20 '11 15:07

Lazloman


People also ask

How to use multiline strings in Perl?

User can create a multiline string using the single(”) quotes and as well as with double quotes(“”). Using double quotes cause variables embedded in the string to be replaced by their content while in single quotes variables name remained the same.

How do I match multiple lines in Perl?

Solution. Use /m , /s , or both as pattern modifiers. /s lets . match newline (normally it doesn't). If the string had more than one line in it, then /foo.

How to match newline in regex Perl?

That default can be changed to add matching the newline by using the single line modifier: for the entire regular expression with the /s modifier, or locally with (? s) (and even globally within the scope of use re '/s' ).

What is =~ in Perl?

The Binding Operator, =~ Matching against $_ is merely the default; the binding operator ( =~ ) tells Perl to match the pattern on the right against the string on the left, instead of matching against $_ .


2 Answers

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.

like image 147
sergio Avatar answered Sep 18 '22 08:09

sergio


print $1 if /(Start Here.*?)End Here/s;
like image 45
tadmc Avatar answered Sep 22 '22 08:09

tadmc