Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: RegEx to filter \s not between labels

by I0 (Priest)
on May 23, 2002 at 00:30 UTC ( [id://168628]=note: print w/replies, xml ) Need Help??


in reply to RegEx to filter \s not between labels

$text =~ s/\s+|(STARTPRESERVE.*?STOPPRESERVE)/${[$1,' ']}[!$1]/gs

UPDATE: jeffa, If you think that's twisted, you should see the version for nested START STOP markers:-)

to remove the markers:
s/\s+|STARTPRESERVE(.*?)STOPPRESERVE/${[' ',$1]}[defined $1]/gs

Replies are listed 'Best First'.
(tye)Re: RegEx to filter \s not between labels
by tye (Sage) on May 23, 2002 at 01:59 UTC

    ++I0! Minor changes to remove the start/stop markers which I think was desired:

    # The golfish way: $text =~ s#(\s)\s+|STARTPRESERVE(.*?)STOPPRESERVE#$1$2#gs; # Update: Oops! This fails if a run # of whitespace doesn't start with ' '. # or, if you don't like warnings: $text =~ s#\s{2,}|STARTPRESERVE(.*?)STOPPRESERVE# defined $1 ? $1 : " " #seg;
    (:

            - tye (but my friends call me "Tye")
Re: Re: RegEx to filter \s not between labels
by gryphon (Abbot) on May 23, 2002 at 16:27 UTC

    Greetings I0,

    ++! That's amazingly cool code. I love one-liners like that. In my specific situation, I don't need the START/STOP markers removed, and I can't imagine a senario where the data string would contain nested labels, however... I'm still curious. How would the nested labels version look?

    (Note that I did spend some time trying to figure this out for myself so I could at least post a few bits of code here for others to revise, but I've failed to figure out even the basic theory without going into many, many lines of basic code.)

    -gryphon
    code('Perl') || die;

      #Okay, you asked for it:
      $_ = '0 1 STARTPRESERVE STARTPRESERVE 2 STOPPRESERVE STOPPRESERVE 3 4 STARTPRESERVE STARTPRESERVE 5 STOPPRESERVE STOPPRESERVE 6 7'; (my $re=$_)=~s/((STARTPRESERVE)|(STOPPRESERVE)|.)/${['(','']}[!$2]\Q$1 +\E${[')','']}[!$3]/gs; $re= join'|',map{quotemeta}eval{/$re/}; die $@ if $@ =~ /unmatched/; s/\s+|($re)/${[$1,' ']}[!$1]/g;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://168628]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-23 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found