Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^4: trim leading & trailing whitespace

by tlm (Prior)
on Mar 30, 2005 at 10:41 UTC ( #443402=note: print w/replies, xml ) Need Help??

in reply to Re^3: trim leading & trailing whitespace
in thread trim leading & trailing whitespace

How about this:

$_ = " yes \n \n \n they \n \n will \n\n\n"; s/^\s*?(?:\b(.*?)\s*?)?(\n)?$/$1$2/gm; print; __END__ % perl yes they will

the lowliest monk

Replies are listed 'Best First'.
Re^5: trim leading & trailing whitespace
by bart (Canon) on Mar 30, 2005 at 11:10 UTC
    Danger, don't use \b if you want to detect the edges between spaces an nonspaces. It won't work for nonspace+nonword characters, like quotes and punctuation.

    Perhaps replace the \b(.*?) with (\S.*?).

    Still, you're not running this with warnings enabled, are you? Both $1 and $2 are optional, so both can be undef, but you use their value regardless. This runs without warnings:


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2016-10-20 21:07 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (283 votes). Check out past polls.