Subject:
|
Strip surplus white space with Perl
|
Newsgroups:
|
lugnet.off-topic.geek
|
Date:
|
Sun, 29 Oct 2000 15:19:29 GMT
|
Viewed:
|
284 times
|
| |
 | |
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. Any suggestions?
Yes, I am a Perl novice... :-)
Fredrik
--
http://www.math.uio.no/~fredrigl/technic/
|
|
Message has 2 Replies:  | | Re: Strip surplus white space with Perl
|
| (...) 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 (...) (25 years ago, 29-Oct-00, to lugnet.off-topic.geek)
|  | | Re: Strip surplus white space with Perl
|
| (...) 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 (...) (25 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
|
|
|
|