Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: trim leading & trailing whitespace

by graff (Chancellor)
on Mar 30, 2005 at 07:56 UTC ( #443364=note: print w/ replies, xml ) Need Help??


in reply to trim leading & trailing whitespace

Personally, I don't see any need to improve on the old standard:

s/^\s+//; s/\s+$//;
update: After seeing thinker's reply I realized I might have misunderstood the question. If, like he suggests, you mean to remove leading and trailing whitespace from each line of a multi-line string, then I'd change the second line above to:
s/\s*\n\s*/\n/g;


Comment on Re: trim leading & trailing whitespace
Select or Download Code
Re^2: trim leading & trailing whitespace
by ady (Deacon) on Mar 30, 2005 at 08:01 UTC
    Probably as good as it gets... Much better than my 1.cut at the job. Tanks a heap! Allan
Re^2: trim leading & trailing whitespace
by ihb (Deacon) on Mar 30, 2005 at 10:09 UTC

    \s matches newlines too, meaning that your pattern will remove empty lines, not just trim them. An alternative is

    s/^[^\S\n]+//gm; s/[^\S\n]+$//gm;

    ihb

    See perltoc if you don't know which perldoc to read!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2015-07-02 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (34 votes), past polls