Shawn H Corey wrote:
jackassplus wrote:
I am trying to remove single newlines but not double newlines.

for instance say I have the following:
Name:\nMy Name\n\nAddress:\n123 Anywhere St\n\nAbout Me:\nSome text
that\nhas some newlines that\nI want to be rid of\n\nThe End\n

I want to get rid of all of the newlines between Me:\n and the next
occurrence of \n\n. and replace them with spaces so it says:
Name:\nMy Name\n\nAddress:\n123 Anywhere St\n\nAbout Me:\nSome text
that has some newlines that I want to be rid of\n\nThe End\n

I'm completely at a loss on how to pull this off...

sub fixer($){
    my $data = $_[0];
    $data =~ s/\\n{1}/ /g; #this is too greedy
                        ^^^
Replace pattern with a single space character.


    print $data;
}

Try:

my $data = "\nMy Name\n\nAddress:\n123 Anywhere St\n\nAbout Me:\nSome
text that\nhas some newlines that\nI want to be rid of\n\nThe End\n";
print "<<$data>>\n";

$data =~ s{ (?<! \n ) \n (?! \n ) }{}gmsx;
                                     ^^
Replace pattern with nothing.  Oh oh!


print "<<$data>>\n";



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to