I'd like to get a substring between two delimiters (regexes) from a string. I want to use this:
while (<>) {
if (/START/../END/) {
next if /START/ || /END/;
print;
}
}
But this works on lines of stdin. I'd like make it work on lines of a string. How?
If you mean you want to process a string that already contains multiple lines then use split:
foreach (split(/\n/, $str)) {
if (/START/../END/) {
next if /START/ || /END/;
print;
}
}
Simply:
my ($between) = $string =~ /START(.*?)END/s;
Alternatively, read from the string:
use 5.010;
open my $string_fh, "<", \$string or die "Couldn't open in-memory file: $!";
while ( <$string_fh> ) {
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With