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

if you know the number of lines between the starting and ending lines of the block of lines you want to elide, something like this might do the trick:

my $starting_line = qr{ ^s [^\n]* \n }xsm; # starts with an 's' my $intervening_line = qr{ [^\n]* \n }xsm; # anything my $ending_line = qr{ e [ ] p \n }xsm; # ends with an 'e p' my $between = 1; my $line = do { local $/; <DATA> }; # slurp all the data $line =~ s{ $start_line ${intervening_line}{$between} $end_line } {}gxsm;

this outputs:

Random String s foo e f blah blah End of file

any closer?

Replies are listed 'Best First'.
Re^2: Matching and replacing the minimum string from the tail of the regex
by Anonymous Monk on Aug 09, 2007 at 01:42 UTC
    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
      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.