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;


In reply to RegEx filter \s ! between labels, part 2 by gryphon

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.