Pathologically Eclectic Rubbish Lister | |
PerlMonks |
RegEx filter \s ! between labels, part 2by gryphon (Abbot) |
on Oct 02, 2002 at 17:39 UTC ( [id://202333]=perlquestion: print w/replies, xml ) | Need Help?? |
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:
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.)
Am I headed in the right direction, or am I missing an easier solution? Thanks all. gryphon
Back to
Seekers of Perl Wisdom
|
|