kamaci
kamaci

Reputation: 75127

How to remove newline character and white spaces after them and replacing them with just whitespace character in Perl?

How can I write a regex pattern with Perl that will work like that:

**If there is a new line**, after it, it will remove that new line character and all the whitespace characters after until it sees any character except for a white space character and will put just one whitespace character instead of them?

Upvotes: 1

Views: 11373

Answers (2)

toolic
toolic

Reputation: 62037

substitution operator

use warnings;
use strict;

my $s = "foo\n    bar goo\nber";
$s =~ s/\n\s*/ /g;
print "$s\n";

__END__

foo bar goo ber

Upvotes: 3

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74202

I am not sure if I understand your question well. Try: s{ \n\s+ }{ }gx

Upvotes: 2

Related Questions