Subject:
|
Re: Strip surplus white space with Perl
|
Newsgroups:
|
lugnet.off-topic.geek
|
Date:
|
Sun, 29 Oct 2000 20:13:16 GMT
|
Viewed:
|
109 times
|
| |
| |
> > What about something like
> >
> > while ($string =~ s/(\s\s+)/substr($&,1,1)/e) {}
>
> 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...
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.
Chris
|
|
Message has 1 Reply: | | Re: Strip surplus white space with Perl
|
| (...) 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)
|
Message is in Reply To:
| | Re: Strip surplus white space with Perl
|
| (...) 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)
|
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
|
|
|
|