gryphon has asked for the wisdom of the Perl Monks concerning the following question:
Greetings fellow monks,
A while back, I sought wisdom regarding a RegEx filter that would filter \s+ from a scalar but skip filtering between to marker labels. I got several really great replies, but I've wandered into a new problem: I need the same filter but allow for multiple marker labels. Here are the details:
I have an large string scalar and I'd like to filter out multiple white spaces, converting them into just a single space per instance. Here's an example of the scalar:
A Bridge Too Far Hosted by Rod Stuart Friendly Skys 42 STARTPRESERVE1 Life, the universe... and Everything STOPPRESER +VE1 Bob HotWheels are cool More movies on Fox File server A Bridge Too Far Hosted by Rod Stuart Friendly Skys 42 STARTPRESERVE2 Life, the universe... and Everything STOPPRESER +VE2 Bob HotWheels are cool More movies on Fox File server
Sounds like a job for $text =~ s/\s+/ /g;, except that I want to avoid the filtering between multiple START and STOP markers. Here's the original code that uses a single pair of markers (provided by IO):
$text =~ s/\s+|(STARTPRESERVE.*?STOPPRESERVE)/${[$1,' ']}[!$1]/gs;This works great exept that I now need multiple markers like STARTPRESERVE1 .. 3 and so forth. My first thought was a loop through an array of these markers doing a regex, but of course that won't work. So I think it needs to be a single regex. (Right?) So here's my feeble attempt: (Please try not to laugh.)
$$text_ref =~ s% (STARTPRESERVE1.*?STOPPRESERVE1) | (STARTPRESERVE2.*?STOPPRESERVE2) | (PRESERVESTART.*?PRESERVESTOP) | \s+ % ((defined $1 and ($1 =~ /^\s+$/)) ? ' ' : $1) . ((defined $2 and ($2 =~ /^\s+$/)) ? ' ' : $2) . ((defined $3 and ($3 =~ /^\s+$/)) ? ' ' : $3) %eigsx;
Am I headed in the right direction, or am I missing an easier solution? Thanks all.
gryphon
code('Perl') || die;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: RegEx filter \s ! between labels, part 2
by fglock (Vicar) on Oct 02, 2002 at 19:55 UTC | |
by gryphon (Abbot) on Oct 03, 2002 at 16:36 UTC | |
|
Re: RegEx filter \s ! between labels, part 2
by fglock (Vicar) on Oct 02, 2002 at 18:00 UTC | |
by merlyn (Sage) on Oct 02, 2002 at 18:02 UTC | |
|
Re: RegEx filter \s ! between labels, part 2
by fglock (Vicar) on Oct 03, 2002 at 18:07 UTC |