You asked about an alternate approach. I have often found that data like you have is best parsed line by line instead of being slurped into a single variable. Of course your mileage will definitely vary!

Anyway a "read the line and throw it away" if not needed is much faster than a "slurp" and substitution. A simple formulation of this is shown below.

Update: I would also add Flipin good, or a total flop? as another way along the way of shem's approach. The three dot (...) version of the "flip flop" syntax works with multiple lines, the (..) version works with single lines.

#!/usr/bin/perl -w use strict; my $line; while ($line = <DATA>) { skip_record() if $line =~ m/^\s*CD_TEXT/; print $line; } sub skip_record { while ($line = (<DATA>), $line !~ m|^\s*//\s*Track|){}; } #prints: # CD_DA # # // Track 1 __DATA__ CD_DA CD_TEXT { LANGUAGE_MAP { 0: 9 } LANGUAGE 0 { TITLE "Multi-01" PERFORMER "" SIZE_INFO { 1, 1, 19, 0, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 7, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0} } } // Track 1

In reply to Re: regex help, please by Marshall
in thread regex help, please by mchampag

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.