one regex to do the work of two?

Discussion in 'Perl Misc' started by Spydo, Jan 19, 2011.

  1. Spydo

    Spydo Guest

    I frequently have to clean up lines with:

    s/^\s+//;
    s/\s+$//;

    Is there ONE regex (as in ONE, SINGLE s///, not one compound
    statement) that can do both of these ?

    Thanks & happy Gnu Year.
     
    Spydo, Jan 19, 2011
    #1
    1. Advertising

  2. Spydo

    Spydo Guest

    On Jan 19, 1:24 pm, Spydo <> wrote:
    > I frequently have to clean up lines with:
    >
    > s/^\s+//;
    > s/\s+$//;
    >
    > Is there ONE regex (as in ONE, SINGLE s///, not one compound
    > statement) that can do both of these ?
    >
    > Thanks & happy Gnu Year.


    DOH Nevermind I found it.. I didnt know ^ and $ could be in parens..

    s/(^\s+)|(\s+$)//g;
     
    Spydo, Jan 19, 2011
    #2
    1. Advertising

  3. >>>>> "Spydo" == Spydo <> writes:

    Spydo> s/^\s+//;
    Spydo> s/\s+$//;

    Spydo> Is there ONE regex (as in ONE, SINGLE s///, not one compound
    Spydo> statement) that can do both of these ?

    Only if you sacrifice runtime efficiency. :)

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.posterous.com/ for Smalltalk discussion
     
    Randal L. Schwartz, Jan 19, 2011
    #3
  4. Spydo <> wrote:
    >I frequently have to clean up lines with:
    >
    >s/^\s+//;
    >s/\s+$//;
    >
    >Is there ONE regex (as in ONE, SINGLE s///, not one compound
    >statement) that can do both of these ?


    Yes, there is but it doesn't make much sense to use it.
    See the relevent FAQ
    perldoc -q "strip blank"
    for details.

    jue
     
    Jürgen Exner, Jan 20, 2011
    #4
  5. Spydo wrote:
    > On Jan 19, 1:24 pm, Spydo<> wrote:
    >> I frequently have to clean up lines with:
    >>
    >> s/^\s+//;
    >> s/\s+$//;
    >>
    >> Is there ONE regex (as in ONE, SINGLE s///, not one compound
    >> statement) that can do both of these ?
    >>
    >> Thanks& happy Gnu Year.

    >
    > DOH Nevermind I found it.. I didnt know ^ and $ could be in parens..
    >
    > s/(^\s+)|(\s+$)//g;


    There is no need for the parentheses

    s/^\s+|\s+$//g;

    Does the same without having to save data to $1 and $2.



    John
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
     
    John W. Krahn, Jan 20, 2011
    #5
  6. Spydo

    Kevin Ryde Guest

    Spydo <> writes:
    >
    > s/(^\s+)|(\s+$)//g;


    Or Regexp::Common::whitespace for a big way of getting a little
    expression. Text::Trim is effective too, in a scary context-sensitive
    do-what-i-mean way.

    (Could the faq cross reference those? If it doesn't make a long entry
    even longer ... :)


    --
    Some people say cricket is like watching grass grow.
    That's not true, it's watching grass die from not watering it.
     
    Kevin Ryde, Jan 20, 2011
    #6
  7. Στις 19/1/2011 8:28 μμ, ο/η Spydo έγÏαψε:
    > On Jan 19, 1:24 pm, Spydo<> wrote:
    >> I frequently have to clean up lines with:
    >>
    >> s/^\s+//;
    >> s/\s+$//;
    >>
    >> Is there ONE regex (as in ONE, SINGLE s///, not one compound
    >> statement) that can do both of these ?
    >>
    >> Thanks& happy Gnu Year.

    >
    > DOH Nevermind I found it.. I didnt know ^ and $ could be in parens..
    >
    > s/(^\s+)|(\s+$)//g;
    >



    here is a pretty one

    s/^\s*(.*?)\s*$/$1/
     
    George Mpouras, Feb 7, 2011
    #7
  8. Spydo

    David Canzi Guest

    In article <iipnm2$1l35$>,
    George Mpouras <> wrote:
    >Στις 19/1/2011 8:28 μμ, ο/η Spydo έγÏαψε:
    >> On Jan 19, 1:24 pm, Spydo<> wrote:
    >>> I frequently have to clean up lines with:
    >>>
    >>> s/^\s+//;
    >>> s/\s+$//;
    >>>
    >>> Is there ONE regex (as in ONE, SINGLE s///, not one compound
    >>> statement) that can do both of these ?
    >>>
    >>> Thanks& happy Gnu Year.

    >>
    >> DOH Nevermind I found it.. I didnt know ^ and $ could be in parens..
    >>
    >> s/(^\s+)|(\s+$)//g;
    >>

    >
    >
    >here is a pretty one
    >
    >s/^\s*(.*?)\s*$/$1/


    That's really neat.

    --
    David Canzi | Life is too short to point out every mistake. |
     
    David Canzi, Feb 9, 2011
    #8
  9. Spydo

    Dr.Ruud Guest

    On 2011-02-09 01:34, David Canzi wrote:
    > In article<iipnm2$1l35$>,
    > George Mpouras<> wrote:
    >> Στις 19/1/2011 8:28 μμ, ο/η Spydo έγÏαψε:
    >>> On Jan 19, 1:24 pm, Spydo<> wrote:


    >>>> I frequently have to clean up lines with:
    >>>>
    >>>> s/^\s+//;
    >>>> s/\s+$//;
    >>>>
    >>>> Is there ONE regex (as in ONE, SINGLE s///, not one compound
    >>>> statement) that can do both of these ?
    >>>>
    >>>> Thanks& happy Gnu Year.
    >>>
    >>> DOH Nevermind I found it.. I didnt know ^ and $ could be in parens..
    >>>
    >>> s/(^\s+)|(\s+$)//g;
    >>>

    >>
    >>
    >> here is a pretty one
    >>
    >> s/^\s*(.*?)\s*$/$1/

    >
    > That's really neat.


    I don't think so, because it also replaces when there is no heading or
    tailing whitespace at all.

    I normally use:

    s/\s+\z//, # remove trailing whitespace
    s/\A\s+//, # remove leading whitespace
    s/[^\S ]/ /g, # replace non-space whitespace by space
    s/ {2,}/ /g, # unify spaces
    for $input;

    See also tr/ in perlop, think about tr/\t\n / /s.

    --
    Ruud
     
    Dr.Ruud, Feb 9, 2011
    #9
  10. Στις 9/2/2011 3:07 πμ, ο/η Dr.Ruud έγÏαψε:
    > On 2011-02-09 01:34, David Canzi wrote:
    >> In article<iipnm2$1l35$>,
    >> George Mpouras<> wrote:
    >>> ΣÄι 19/1/2011 8:28 μμ, ο/η Spydo έγÃÂαÈε:
    >>>> On Jan 19, 1:24 pm, Spydo<> wrote:

    >
    >>>>> I frequently have to clean up lines with:
    >>>>>
    >>>>> s/^\s+//;
    >>>>> s/\s+$//;
    >>>>>
    >>>>> Is there ONE regex (as in ONE, SINGLE s///, not one compound
    >>>>> statement) that can do both of these ?
    >>>>>
    >>>>> Thanks& happy Gnu Year.
    >>>>
    >>>> DOH Nevermind I found it.. I didnt know ^ and $ could be in parens..
    >>>>
    >>>> s/(^\s+)|(\s+$)//g;
    >>>>
    >>>
    >>>
    >>> here is a pretty one
    >>>
    >>> s/^\s*(.*?)\s*$/$1/

    >>
    >> That's really neat.

    >
    > I don't think so, because it also replaces when there is no heading or
    > tailing whitespace at all.
    >
    > I normally use:
    >
    > s/\s+\z//, # remove trailing whitespace
    > s/\A\s+//, # remove leading whitespace
    > s/[^\S ]/ /g, # replace non-space whitespace by space
    > s/ {2,}/ /g, # unify spaces
    > for $input;
    >
    > See also tr/ in perlop, think about tr/\t\n / /s.
    >



    too much noise for something simple
     
    George Mpouras, Feb 9, 2011
    #10
  11. Spydo

    Guest

    On Wed, 09 Feb 2011 02:07:24 +0100, "Dr.Ruud" <> wrote:

    >On 2011-02-09 01:34, David Canzi wrote:
    >> In article<iipnm2$1l35$>,
    >> George Mpouras<> wrote:
    >>> Στις 19/1/2011 8:28 μμ, ο/η Spydo έγÏ?αψε:
    >>>> On Jan 19, 1:24 pm, Spydo<> wrote:

    >
    >>>>> I frequently have to clean up lines with:
    >>>>>
    >>>>> s/^\s+//;
    >>>>> s/\s+$//;
    >>>>>
    >>>>> Is there ONE regex (as in ONE, SINGLE s///, not one compound
    >>>>> statement) that can do both of these ?
    >>>>>
    >>>>> Thanks& happy Gnu Year.
    >>>>
    >>>> DOH Nevermind I found it.. I didnt know ^ and $ could be in parens..
    >>>>
    >>>> s/(^\s+)|(\s+$)//g;
    >>>>
    >>>
    >>>
    >>> here is a pretty one
    >>>
    >>> s/^\s*(.*?)\s*$/$1/

    >>
    >> That's really neat.

    >
    >I don't think so, because it also replaces when there is no heading or
    >tailing whitespace at all.
    >
    >I normally use:
    >
    > s/\s+\z//, # remove trailing whitespace
    > s/\A\s+//, # remove leading whitespace
    > s/[^\S ]/ /g, # replace non-space whitespace by space
    > s/ {2,}/ /g, # unify spaces
    > for $input;
    >
    >See also tr/ in perlop, think about tr/\t\n / /s.


    This is pretty far afield from removing leading/trailing
    whitespace.

    -sln
     
    , Feb 10, 2011
    #11
    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. news.frontiernet.net
    Replies:
    6
    Views:
    1,168
    news.frontiernet.net
    Apr 16, 2004
  2. GenxLogic
    Replies:
    3
    Views:
    1,371
    andrewmcdonagh
    Dec 6, 2006
  3. TtfnJohn
    Replies:
    1
    Views:
    318
    Peter Otten
    Jun 11, 2007
  4. Replies:
    3
    Views:
    823
    Reedick, Andrew
    Jul 1, 2008
  5. Vincent Mouton

    regex: match at least one of two expression

    Vincent Mouton, Oct 12, 2004, in forum: Perl Misc
    Replies:
    8
    Views:
    117
    Gerhard M
    Oct 12, 2004
Loading...

Share This Page