in reply to Re: Matching and replacing the minimum string from the tail of the regex
in thread Matching and replacing the minimum string from the tail of the regex

alternatively, if it is known that the intervening line(s) will never begin with some pattern:

my $starter = qr{ s }xsm; # starts with this string my $never = qr{ s }xsm; # never starts with this string my $ender = qr{ e [ ] p }xsm; # ends with this string my $start_line = qr{ ^ $starter [^\n]* \n }xsm; my $intervening_line = qr{ ^ (?! $never ) [^\n]* \n }xsm; my $end_line = qr{ $ender \n }xsm; my $line = do { local $/; <DATA> }; # slurp all the data $line =~ s{ $start_line $intervening_line* $end_line } {}gxsm; print $line; __DATA__ Random String s erartt e p s foo e f blah blah s adflkja wibble wobble e p End of file

output:

Random String s foo e f blah blah End of file

Replies are listed 'Best First'.
Re^3: Matching and replacing the minimum string from the tail of the regex
by abitkin (Monk) on Aug 09, 2007 at 14:13 UTC
    For some reason, I had trouble with your code. Instead I turned the problem on it's head.
    use strict; my $lines = do{ local $/; <DATA> }; # reverse the order of the lines so that the RE matches the # last part of the region first my $reversetext = join("\n", reverse(split("\n",$lines))); $reversetext =~ s/^[^\n]*e p.*?s[^\n]*\n//msg; # put the lines in normal order again $lines = join("\n",reverse(split("\n", $reversetext))); print $lines; __DATA__ Random String s erartt e p s foo e f blah blah s adflkja wibble wobble e p End of file

    ==
    Kwyjibo. A big, dumb, balding North American ape. With no chin.