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 / 2235
    Strip surplus white space with Perl —Fredrik Glöckner
   Hi, I'm looking into a quick and simple way to strip off all surplus white space from a string with Perl. 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. (...) (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)
   
        Re: Strip surplus white space with Perl —Christopher Lindsey
     (...) What about something like while ($string =~ s/(\s\s+)/substr($&,1,1)/e) {} ? That should replace all whitespace ([ \t\n\r\f]) that's repeated one or more times (that's the first part of the expression) with the first character matching in that (...) (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)
    
         Re: Strip surplus white space with Perl —David Eaton
     (...) Actually, I think a simpler way is: $string =~ s/(\s)\s+/$1/g; The 'g' option at the end of the regexp will actually just do the replace everywhere in the string that it can, with no real need to loop... Ain't TMTOWTDI great? :) DaveE (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)
    
         Re: Strip surplus white space with Perl —Christopher Lindsey
     (...) 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)
    
         Re: Strip surplus white space with Perl —Christopher Lindsey
     (...) 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+)...&,1,1)/eg; Yes, (...) (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)
    
         Re: Strip surplus white space with Perl —Todd Lehman
     (...) 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)
    
         Re: Strip surplus white space with Perl —Todd Lehman
      (...) s/009/011/ oops :) --Todd (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)
    
         Re: Strip surplus white space with Perl —Christopher Lindsey
     (...) There are some days when I shouldn't touch a keyboard. I read and reread his initial posting and could have sworn that it said "It *does* matter to me which one..." I think you just went in and edited the message. ;) I guess it's one of those (...) (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)
    
         Re: Strip surplus white space with Perl —Dan Boger
     On Sun, Oct 29, 2000 at 10:00:13PM +0000, Christopher Lindsey wrote: [snip] (...) of course, if you're using $&, you don't care about efficiency anyway, so you might as well put your regexp in a while loop. But if you do care about speed, you'd go (...) (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)
   
        Re: Strip surplus white space with Perl —Todd Lehman
   (...) First, if you want to trim any leading and trailing whitespace, this'll do it: $foo =~ s/^\s+//; $foo =~ s/\s+$//; Then, if you need to preserve the first whitespace character and throw out the rest: $foo =~ s/(\s)\s+/$1/g; Or, if spaces (...) (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)
 

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