Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: regex doubt on excluding

by hdb (Monsignor)
on Apr 21, 2014 at 08:40 UTC ( #1082994=note: print w/replies, xml ) Need Help??


in reply to regex doubt on excluding

A question on whitespace is difficult to address as one cannot see the objects of interest. Your regex suggests that in a multiline string you want to replace lines consisting only of whitespaces with truly empty lines. Whitespaces in non-empty lines will be preserved. I am replacing the whitespace with 'x' to see where we got matches:

use strict; use warnings; my $string = "next line is spaces next line is tabs and now some newlines end"; $string =~ s/^\s*$/x/mg; print "$string\n";
The result of this would be:
next line is spaces x next line is tabs x and now some newlines xx end
When you say you want to exclude newlines from the match, what is the desired effect you want to see? Preserve multiple empty lines?

Replies are listed 'Best First'.
Re^2: regex doubt on excluding
by Anonymous Monk on Apr 21, 2014 at 08:44 UTC
    Yes, that's it, Preserve multiple empty lines. (but without white-spaces)

      How about this?

      $string =~ s/^\s*?\n$/\n/mg;

      UPDATE: the regex is not working properly but this should, the \n and the $ are somewhat duplicate:

      $string =~ s/^\s*?\n/\n/mg;

        Thank you hdb... it worked...

        its working with $string =~ s/^\s*?\n/\n/mg; too... :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1082994]
help
Chatterbox?
[marto]: trench humour folks, feeling rough.
[marto]: there is never a night when they sleep all the way through, but last night was something else
[marto]: gave in at 3:45 ish and let Charlie watch videos about spitfires, the battle of Britain. He's plane crazy
[1nickt]: marto soothing!
[marto]: Hiromi makes an appearance, let's hope Jools doesn't ruin it by joining in on the old Joanna
[1nickt]: karlgoethebier What is the issue with the semic-colon after the ellipsis? It's documented as proper syntax ...;
[karlgoethebier]: Crazy? What should i say? Son just started his third studies. Next week i have a session with my therapist ;-)
[marto]: and just as I type that, the boys begin playing the piano, the price I pay for using cockney slang :P

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2017-11-18 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (277 votes). Check out past polls.

    Notices?