Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple \n in regex

Status
Not open for further replies.

AMiSM

Technical User
Jan 26, 2006
128
US
I'm trying to remove multiple newlines from a variable using a regular expression. I've tried:

$text =~ s/\n+/\n/gms;
$text =~ s/\n+/\n/gs;

...but nothing works. I'm certain that there is nothing between each newline.

What am I doing wrong?
 
hmmm

try this

while(/\n/){
s/\n//;
}

Mike

When working on any project the value of other people is exactly that - they are other people. The mismatch between their views and the view you've been contentedly assuming is right, is where that value lies.
 
In both instance your replacing \n with \n

$text =~ s/\n+/\n/gms;
$text =~ s/\n+/\n/gs;

$text =~ s/\n//g; replace \n with nothing.
 
it looks like they are collapsing multiple newlines into one newline. Might try:

$text =~ s/[\r\n]+/\n/gs;


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ok, I feel really stupid. I overlooked a bit of code that made some more \n's after others were removed. Sorry folks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top