Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: trim leading & trailing whitespace

by thinker (Parson)
on Mar 30, 2005 at 10:04 UTC ( #443390=note: print w/replies, xml ) Need Help??


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

Hi bart

In the example I gave above,

 s/^\s*(.*?)\s*$/$1/gm

the combination of the \m modifier, and the line anchors ^ and $ will ensure the newlines are left alone

cheers

thinker

Replies are listed 'Best First'.
Re^3: trim leading & trailing whitespace
by bart (Canon) on Mar 30, 2005 at 10:08 UTC
    $_ = " no \n \n \n they \n \n won't \n\n\n"; s/^\s*(.*?)\s*$/$1/gm; print;
    Result:
    no
    they
    won't
    

    All blank lines are gone.

      Hi bart,

      I see what you mean now. I thought you were suggesting it would remove the newline characters, not just the blank lines. Your way is more correct.

      cheers

      thinker

      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 443391.pl yes they will

      the lowliest monk

        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:

        s/^\s*?((?:\S.*?)?)\s*?(\n?)$/$1$2/gm;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://443390]
help
Chatterbox?
[LanX]: Sinai_Peninsula
[robby_dobby]: choroba: If you're talking of Eurasia, it's only true of the erstwhile Constantinople/ Ottoman empire
[LanX]: yes correct, but Sinai is not on the other side
[LanX]: Organizing a YAPC in a tourist ressort is not a bad idea ...
[LanX]: ... and we could swap to our first YAPC Africa the following year when organizing in Hurghada
[robby_dobby]: LanX: Well volunteered!
[LanX]: ... just ... ppl would be to afraid of terrorist attacks
[choroba]: python guerrila?
[robby_dobby]: if I do make it, I can at least savour their local tea! :-)
[LanX]: oh they have sleper cells everywhere

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (15)
As of 2017-04-24 16:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (442 votes). Check out past polls.