To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.off-topic.geekOpen lugnet.off-topic.geek in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Off-Topic / Geek / 2245
2244  |  2246
Subject: 
Re: Strip surplus white space with Perl
Newsgroups: 
lugnet.off-topic.geek
Date: 
Sun, 29 Oct 2000 20:30:48 GMT
Viewed: 
126 times
  
Christopher Lindsey wrote:

Ahh, but what happens if you have a carriage return followed by a
space?  The second method eliminates the space and leaves the
carriage return, which isn't what he asked for.  He wanted a way
to replace multiple instances of similar whitespace with a single
instance of the same whitespace type.  i.e.

   \t\t\t becomes \t
   \s\s\s becomes \s
   \t\t\s\s becomes \t\s
   etc.

I hate replying to myself, but I realize that I pasted the
wrong answer into my initial response now, and totally missed
the boat on my response to David.  Ooops.  :)

This should do it:

   $string =~ s/(\t\t+|  +|\r\r+|\n\n+|\f\f+)/substr($&,1,1)/eg;

Yes, a /g will work here, David was right about that.  I usually stick
with a while loop because in some cases it's better (i.e. if you're
using $` and don't want to recalculate things like length from the start
of the string each time).

Chris



Message has 1 Reply:
  Re: Strip surplus white space with Perl
 
(...) Ahh, but that *is* what he asked for. Fredrik said, "If two consecutive characters are <SPACE> and <RET>, for example, it doesn't matter to me which one is preserved and which one is chopped off." (...) My goodness. I suppose you could do it (...) (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)

Message is in Reply To:
  Re: Strip surplus white space with Perl
 
(...) Ahh, but what happens if you have a carriage return followed by a space? The second method eliminates the space and leaves the carriage return, which isn't what he asked for. He wanted a way to replace multiple instances of similar whitespace (...) (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)

10 Messages in This Thread:



Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR