Using s///g to remove carriage returns

Discussion in 'Perl Misc' started by Jason, Aug 5, 2006.

  1. Jason

    Jason Guest

    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
     
    Jason, Aug 5, 2006
    #1
    1. Advertising

  2. Jason

    Xicheng Jia Guest

    Jason wrote:
    > 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
     
    Xicheng Jia, Aug 5, 2006
    #2
    1. Advertising

  3. Jason

    Dr.Ruud Guest

    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?

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Aug 5, 2006
    #3
  4. Jason wrote:
    > 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.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Aug 7, 2006
    #4
  5. Jason

    Adam Funk Guest

    On 2006-08-05, Jason <> wrote:

    > 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.
     
    Adam Funk, Aug 7, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iceberg

    Carriage Returns and sockets

    Iceberg, Sep 6, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,165
    Iceberg
    Sep 6, 2003
  2. Replies:
    1
    Views:
    2,758
    Martin Dechev
    Oct 7, 2005
  3. Simon Harris

    Remove Carriage Returns

    Simon Harris, Apr 27, 2005, in forum: ASP General
    Replies:
    3
    Views:
    126
    Simon Harris
    Apr 29, 2005
  4. Steve Anderson
    Replies:
    3
    Views:
    262
    Steve Anderson
    Jun 21, 2004
  5. norfernuman

    remove carriage returns

    norfernuman, Jul 1, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    125
    John J. Trammell
    Jul 1, 2004
Loading...

Share This Page