http://www.perlmonks.org?node_id=443368


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?