In your TestEK.txt file, you have all  {[%tqu ... ]} sequences on the same line. In the second, real file, these sequences span two or more lines.

You are processing the file line-by-line and matching your regex against each line, so if a  {[%tqu ... ]} sequence spans multiple lines, the regex will never see it.

If the file is small, less than, say, several hundred megabytes and never likely to grow larger, it might be easiest to "slurp" the entire file at once as a string into a scalar variable and then do a single  s/// against the variable, then write the string back out to the new file.
    my $string = do { local $/;  <>; };
    $string =~ s/$regex/$subst/gis;
    print $string;
(untested). Note that the  s/// now needs a  /s regex modifier so that  . (dot) in  .* will match a newline across multiple lines. Get rid of the while-loop entirely.

Update: See also File::Slurp.

Update 2: Here's a test:

c:\@Work\Perl\monks\OldChamp>perl -wMstrict -le "my $s = do { local $/; <>; }; print qq{[[$s]] \n}; ;; my $rx = '{\[%tqu.*]}'; my $su = ''; $s =~ s/$rx/$su/gis; print qq{[[$s]]}; " tqu.txt [[keep this {[%tqu get rid of this]} and keep this too ]] [[keep this and keep this too ]]

Update 3: Update 2 contains a rookie mistake: using greedy  .* instead of the lazy  .*? version. Here's a version that will actually work with a single long string. The previous version would delete everything between the absolute first  {[%tqu and the absolute last  ]} sequence in the file.

c:\@Work\Perl\monks\OldChamp>perl -wMstrict -le "my $s = do { local $/; <>; }; print qq{[[$s]] \n}; ;; my $rx = '{\[%tqu.*?]}'; my $su = ''; $s =~ s/$rx/$su/gis; print qq{[[$s]]}; " tqu.txt [[keep this {[%tqu get rid of this]} and keep this too keep {[%tqu but dump also ]} it to here. ]] [[keep this and keep this too keep it to here. ]]
Actually, something like
    my $regex = qr{ {\[%tqu [^\]]* ]} }xms;
might even be preferable as long as there are guaranteed to be no  ] (right-square-bracket) characters in the sub-strings to be removed, but let's leave it at that for now.


Give a man a fish:  <%-{-{-{-<


In reply to Re^3: Substitution don't work by AnomalousMonk
in thread Substitution don't work by OldChamp

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.