Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: trim leading & trailing whitespace

by BrowserUk (Pope)
on Mar 30, 2005 at 08:06 UTC ( #443368=note: print w/replies, xml ) Need Help??


in reply to trim leading & trailing whitespace

s[^\s*(.*)\b\s*$][$1]

Update: Corion rightly points out that this does not work correctly for lines where the last non whitespace character is a non-words character. Eg.

the quick brown fox ( \n # ^

So, use s[^\s*(.*\S)\s*$][]; instead. It's slower, but still quicker than the FAQ method.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco.
Rule 1 has a caveat! -- Who broke the cabal?

Replies are listed 'Best First'.
Re^2: trim leading & trailing whitespace
by ady (Deacon) on Mar 30, 2005 at 08:46 UTC
    Good one too (needs the gm modifiers to do the job tho') -- allan

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://443368]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2017-03-23 23:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (294 votes). Check out past polls.