in reply to regex help, please

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

Replies are listed 'Best First'.
Re^2: regex help, please
by mchampag (Acolyte) on Dec 21, 2009 at 18:18 UTC

    Thank you very much for both the line-by-line test and flip-flop hints. This problem had to do with fixing an existing script which choked on unexpected input (the CD_TEXT field), and so I was looking for as non-invasive a solution as possible, but I'll definitely keep your tips in mind for next time.

    -Matt