in reply to Replacing everything in between using s///;

An s flag tells the regex to ignore line boundaries, which it won't do normally. The following works fine:
$string =~ s/$first(.*?)$last/$reserve/s;
And since you aren't actually using the part in the middle, you don't need the parentheses, though the code works fine with or without them:
$string =~ s/$first.*?$last/$reserve/s;

Replies are listed 'Best First'.
Re^2: Replacing everything in between using s///;
by tlm (Prior) on May 09, 2005 at 04:58 UTC

    An s flag tells the regex to ignore line boundaries, which it won't do normally.

    I wouldn't put it that way. /s makes newlines a valid match for ".". Moreover, perl normally does ignore internal line boundaries unless one tells it not to with /m. E.g.

    my $s = "a\nb\nc\n"; my @g = map "[$_]", $s =~ /(.)$/g; my @gs = map "[$_]", $s =~ /(.)$/gs; my @gm = map "[$_]", $s =~ /(.)$/gm; my @gsm = map "[$_]", $s =~ /(.)$/gsm; print "g: @g\n"; print "gs: @gs\n"; print "gm: @gm\n"; print "gsm: @gsm\n"; __END__ g: [c] gs: [c] [ ] gm: [a] [b] [c] gsm: [a] [b] [c] [ ]
    Note in particular the case in which both /s and /m are used.

    the lowliest monk