in reply to Search and replace over line break

As long as you can guarantee that the substring to match will not extend over more than two lines, you could use a two-line sliding buffer that you apply the substitution to, but then split again before outputting (so you won't get everything twice):

#!/usr/bin/perl use strict; use warnings; my $prev = ""; my $done = 0; while (<DATA>) { # sliding two-line buffer $_ = $prev.$_; # get rid of middle newline, so it doesn't interfere with match s/\n//; s/<\?string\?>/<xxx>/g; # split buf # first part will be output, second part kept # for joining with next input line ($_, $prev) = unpack "a".length($prev)."a*", $_; print "$_\n" if 2 .. $done; # corner case: have last line be printed as well if (eof and !$done++) { $_ = ""; redo } } __DATA__ hello there this is a bad <?string?> that we need to take away. Here is another bad <?st ring?> that needs to go too.

Output:

hello there this is a bad <xxx> that we need to take away. Here is another bad <xxx > that needs to go too.

As you can see, line breaks may be occur anywhere in the middle of the substitution. If that's a problem, you'd have to modify the logic of splitting the sliding buffer (which can get tricky...).

(There may be corner cases I've overlooked, but you get idea...)