How to process a multi-line row string at a time

I would like to get a substring between two delimiters (regular expressions) from a string. I want to use this:

while (<>) {
  if (/START/../END/) {
    next if /START/ || /END/;
    print;
  }
}

But this works on stdin lines. I would like to make it work on line by line lines. How?

+3
source share
3 answers

If you want to process a line that already contains several lines, use split:

foreach (split(/\n/, $str)) {
  if (/START/../END/) {
    next if /START/ || /END/;
    print;
  }
}
+5
source

Simply:

my ($between) = $string =~ /START(.*?)END/s;

Alternatively, read from the line:

use 5.010;
open my $string_fh, "<", \$string or die "Couldn't open in-memory file: $!";
while ( <$string_fh> ) {
    ...
+3
source

. , , $/ (, " undef ), .

. "" @perldoc

0

All Articles