Subject:
|
Strip surplus white space with Perl
|
Newsgroups:
|
lugnet.off-topic.geek
|
Date:
|
Sun, 29 Oct 2000 15:19:29 GMT
|
Viewed:
|
61 times
|
| |
![Post a public reply to this message](/news/icon-reply.gif) | |
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: ![](/news/x.gif) | | 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 (...) (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)
| ![](/news/x.gif) | | 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 (...) (24 years ago, 29-Oct-00, to lugnet.off-topic.geek)
|
10 Messages in This Thread: ![You are here](/news/here.gif) ![](/news/246.gif) ![Re: Strip surplus white space with Perl -Christopher Lindsey (29-Oct-00 to lugnet.off-topic.geek)](/news/x.gif) ![](/news/46.gif) ![Re: Strip surplus white space with Perl -David Eaton (29-Oct-00 to lugnet.off-topic.geek)](/news/x.gif) ![](/news/46.gif) ![Re: Strip surplus white space with Perl -Christopher Lindsey (29-Oct-00 to lugnet.off-topic.geek)](/news/x.gif) ![](/news/46.gif) ![Re: Strip surplus white space with Perl -Christopher Lindsey (29-Oct-00 to lugnet.off-topic.geek)](/news/x.gif) ![](/news/46.gif) ![Re: Strip surplus white space with Perl -Todd Lehman (29-Oct-00 to lugnet.off-topic.geek)](/news/x.gif) ![](/news/246.gif) ![Re: Strip surplus white space with Perl -Todd Lehman (29-Oct-00 to lugnet.off-topic.geek)](/news/x.gif)
![](/news/x.gif) ![](/news/28.gif) ![](/news/x.gif) ![](/news/x.gif) ![](/news/68.gif) ![Re: Strip surplus white space with Perl -Christopher Lindsey (29-Oct-00 to lugnet.off-topic.geek)](/news/x.gif) ![](/news/46.gif) ![Re: Strip surplus white space with Perl -Dan Boger (29-Oct-00 to lugnet.off-topic.geek)](/news/x.gif)
![](/news/x.gif) ![](/news/68.gif) ![Re: Strip surplus white space with Perl -Todd Lehman (29-Oct-00 to lugnet.off-topic.geek)](/news/x.gif)
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|