in reply to delete multiple string blocks
Here's another approach. You already seem to have a solution you like and this may be too regexy for your taste, but FWIW... Note this requires Perl version 5.10+ for certain regex extensions; see Extended Patterns. See Building Regex Alternations Dynamically for more info on building the $rx_target part of the search regex. (OPed example data file used for testing.)
use 5.010; # need (?PARNO) and possessive quantifier regex extensions use warnings; use strict; use autodie; use constant USAGE => <<"EOUSAGE"; usage: $0 filename target [ target ... ] where: filename required: name of file to be processed target required (1 or more): target block(s) to be deleted EOUSAGE MAIN: { die "no filename given \n", USAGE unless @ARGV >= 1; die "no target(s) given \n", USAGE unless @ARGV >= 2; my ($filename, @targets) = @ARGV; my $content = do { open my $fh, '<', $filename; local $/; <$fh>; }; print "before: >>$content<< \n\n"; # for debug my ($rx_target) = # build search regex map qr{ \b (?: $_) \b }xms, join '|', map quotemeta, reverse sort @targets ; print "search regex: $rx_target \n\n"; # for debug $content =~ s{ $rx_target \s+ [[:alpha:]]+ \s* ( \{ # begin capture group 1 (balanced curlies) (?: [^{}]*+ | (?1))* \} \s* ) # end capture group 1 } {}xmsg; print "after: >>$content<< \n\n"; # for debug exit; # successful exit from MAIN } # end MAIN block die "unexpected exit from MAIN"; # subroutines ###################################################### # none for now
Update 1: Because I don't know just what you want to do with it, I'm not actually outputting the processed file content (except for debug prints); you'll have to take care of that yourself.
Update 2: If a balanced expression regex utility is used, the code gets simpler/clearer (see Regexp::Common and Regexp::Common::balanced):
use Regexp::Common; ... $content =~ s{ $rx_target \s+ [[:alpha:]]+ \s* $RE{balanced}{-parens=>'{}'} \s* } {}xmsg; ...
Give a man a fish: <%-{-{-{-<
|
|---|