Using s///g to remove carriage returns

J

Jason

I've gone through about a dozen options, and none of them work
correctly! This is driving me nuts.

The request is pretty simple: I have a form with a textarea field, and
I want to remove any opening or trailing carriage returns from it
(while leaving any return in the middle). This is the code I tried most
recently, and was certain would work:

$comment =~ s/^\r\n+|^\r+|^\n+|\r\n+$|\r+$|\n+$//g;

But, it doesn't seem to have any impact at all! Correct me if I'm
wrong, but I thought that this broke down like:

s/ # substitute
^\r\n+| # start at the beginning, find all \r\n until you find
something else, OR
^\r+| # same as above, finding only \r, OR
^\n+| # same, finding only \n, OR
\r\n+$| # finding all \r\n that go to the end uninterrupted, OR
\r+$| # same, finding only \r, OR
\n+$ # same, finding only \n
// # replace any of the above with nothing
g; # do this globally

I also tried breaking it down into 2 separate recipes, which had no
impact:

$comment =~ s/^\r\n+|^\r+|^\n+//g;
$comment =~ s/\r\n+$|\r+$|\n+$//g;


Not surprisingly, this one DOES work to remove whitespaces (taken from
the FAQ):

$comment =~ s/^\s+|\s+$//g;

This should be the same thing as my recipe above, but instead of \s,
I've used \r, \n, or some variation.

Do you guys see what I'm overlooking?

TIA,

Jason
 
X

Xicheng Jia

Jason said:
I've gone through about a dozen options, and none of them work
correctly! This is driving me nuts.

The request is pretty simple: I have a form with a textarea field, and
I want to remove any opening or trailing carriage returns from it
(while leaving any return in the middle). This is the code I tried most
recently, and was certain would work:

$comment =~ s/^\r\n+|^\r+|^\n+|\r\n+$|\r+$|\n+$//g;

But, it doesn't seem to have any impact at all! Correct me if I'm
wrong, but I thought that this broke down like:

s/ # substitute
^\r\n+| # start at the beginning, find all \r\n until you find
something else, OR
^\r+| # same as above, finding only \r, OR
^\n+| # same, finding only \n, OR
\r\n+$| # finding all \r\n that go to the end uninterrupted, OR
\r+$| # same, finding only \r, OR
\n+$ # same, finding only \n
// # replace any of the above with nothing
g; # do this globally

I also tried breaking it down into 2 separate recipes, which had no
impact:

$comment =~ s/^\r\n+|^\r+|^\n+//g;
$comment =~ s/\r\n+$|\r+$|\n+$//g;


Not surprisingly, this one DOES work to remove whitespaces (taken from
the FAQ):

$comment =~ s/^\s+|\s+$//g;

This should be the same thing as my recipe above, but instead of \s,
I've used \r, \n, or some variation.

Do you guys see what I'm overlooking?

bash~ $ perl -e '
$_="\n\n\n\r\rI am \n\n\r\r";
s/^\r\n+|^\r+|^\n+|\r\n+$|\r+$|\n+$//g;
print' | cat -A
___print___
^M^MI am $
$
__________
So you know what your regex does, don't you?

If you want to remove both \r and \n in any order, try the following:

$comment =~ s/^[\r\n]+|[\r\n]+$//g;

Xicheng
 
D

Dr.Ruud

Jason schreef:
none of them work correctly!

ITYM: none of them work as you expected.

^\r\n+| # start at the beginning, find all \r\n until you find
something else, OR

ITYM: ^(?:\r\n)+

\r\n+$| # finding all \r\n that go to the end uninterrupted, OR

ITYM: (?:\r\n)+$


Still, if

s{ ^\s+ | \s+$ }{}xg

works, then why complicate things?
 
J

Josef Moellers

Jason said:
I've gone through about a dozen options, and none of them work
correctly! This is driving me nuts.

The request is pretty simple: I have a form with a textarea field, and
I want to remove any opening or trailing carriage returns from it
(while leaving any return in the middle). This is the code I tried most
recently, and was certain would work:

$comment =~ s/^\r\n+|^\r+|^\n+|\r\n+$|\r+$|\n+$//g;

But, it doesn't seem to have any impact at all! Correct me if I'm
wrong, but I thought that this broke down like:
Do you guys see what I'm overlooking?

In a nutshell: the precedence of operators (concatenation is an
operator) in regular expressions.
 
A

Adam Funk

The request is pretty simple: I have a form with a textarea field, and
I want to remove any opening or trailing carriage returns from it
(while leaving any return in the middle). This is the code I tried most
recently, and was certain would work:

$comment =~ s/^\r\n+|^\r+|^\n+|\r\n+$|\r+$|\n+$//g;

I think the easiest way to do this is in two steps:

$comment =~ s/^[\r\n]+//;
$comment =~ s/[\r\n]+$//;

I'm assuming that you want to delete all "carriage returns" and
"newlines" from the beginning and end, without distinguishing between
the two things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top