Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: It's all getting messy - remove whitespace

by FloydATC (Chaplain)
on Jun 15, 2014 at 10:45 UTC ( #1089932=note: print w/ replies, xml ) Need Help??


in reply to It's all getting messy - remove whitespace

The regular expression \s+$ means "one or more whitespace characters at the end of the line" so it will never match whitespaces in between other characters.

Whenever I want to strip excessive whitespace from a string, I usually go about it in a three-step way:
  1. Combine multiple whitespaces into a single space using s/\s+/ /g;
  2. Remove the leading space (if any) with s/^\s//;
  3. Remove the trailing space (if any) with s/\s$//; (Remember that this assumes no CR/LF character, adjust as needed)

Add a few comments and the job is done.

Perhaps this can be combined into a single-shot regex but it will probably be harder to read and waste more CPU cycles to accomplish exactly the same thing. I never bothered to find out because this approach works for me.

-- FloydATC

Time flies when you don't know what you're doing


Comment on Re: It's all getting messy - remove whitespace
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1089932]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-11-23 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (131 votes), past polls