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:  <%-{-{-{-<


In reply to Re: delete multiple string blocks (updated) by AnomalousMonk
in thread delete multiple string blocks by grandagent

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.