not greedy enough

Discussion in 'Ruby' started by trans. (T. Onoma), Sep 22, 2004.

  1. Honestly, I really dislike regular expressions. Some people love them for
    their terseness. I, on the other hand, being a pooh of very little brain,
    spend far too many hair pulling hours just trying to get one of the dang
    things right. Ugh.

    Right now I'm working on this:

    /^[ ]*[-](.*\n)+.*\n/

    Applied to:

    %Q{
    - xyz
    - abc

    - not this
    }

    I want it to match just the first section, essentially

    " - xyz\n - abc\n\n".

    But it's matching the whole string. Can any of you Regexp experts fill me in?

    Thanks,
    T.
    trans. (T. Onoma), Sep 22, 2004
    #1
    1. Advertising

  2. Hi --

    On Thu, 23 Sep 2004, trans. (T. Onoma) wrote:

    > Right now I'm working on this:
    >
    > /^[ ]*[-](.*\n)+.*\n/
    >
    > Applied to:
    >
    > %Q{
    > - xyz
    > - abc
    >
    > - not this
    > }
    >
    > I want it to match just the first section, essentially
    >
    > " - xyz\n - abc\n\n".
    >
    > But it's matching the whole string. Can any of you Regexp experts fill me in?


    I think what you want is: match one or more consecutive occurrences
    of <space>-<stuff>\n, stopping when one of them has an extra \n at the
    end.

    Try this and see if it's close to what you need:

    /(^\ *-.*\n)+\n/


    David

    --
    David A. Black
    David A. Black, Sep 22, 2004
    #2
    1. Advertising

  3. trans.  (T. Onoma)

    Hal Fulton Guest

    David A. Black wrote:
    > I think what you want is: match one or more consecutive occurrences
    > of <space>-<stuff>\n, stopping when one of them has an extra \n at the
    > end.
    >
    > Try this and see if it's close to what you need:
    >
    > /(^\ *-.*\n)+\n/


    Umm... in the regexp as in the other, isn't an m needed at the end?


    Hal
    Hal Fulton, Sep 22, 2004
    #3
  4. trans.  (T. Onoma)

    Hal Fulton Guest

    Hal Fulton wrote:
    > David A. Black wrote:
    >
    >> I think what you want is: match one or more consecutive occurrences
    >> of <space>-<stuff>\n, stopping when one of them has an extra \n at the
    >> end.
    >>
    >> Try this and see if it's close to what you need:
    >>
    >> /(^\ *-.*\n)+\n/

    >
    >
    > Umm... in the regexp as in the other, isn't an m needed at the end?


    No, sorry, ignore me. I was quite wrong.

    Hal
    Hal Fulton, Sep 22, 2004
    #4
  5. Hi --

    On Thu, 23 Sep 2004, Hal Fulton wrote:

    > David A. Black wrote:
    > > I think what you want is: match one or more consecutive occurrences
    > > of <space>-<stuff>\n, stopping when one of them has an extra \n at the
    > > end.
    > >
    > > Try this and see if it's close to what you need:
    > >
    > > /(^\ *-.*\n)+\n/

    >
    > Umm... in the regexp as in the other, isn't an m needed at the end?


    No, because if you put an /m, then the dot will match \n, and .* will
    trample over the end of the lines. I'm using \n as a flag for
    starting and stopping.


    David

    --
    David A. Black
    David A. Black, Sep 22, 2004
    #5
  6. On Wednesday 22 September 2004 03:36 pm, David A. Black wrote:
    > I think what you want is: match one or more consecutive occurrences
    > of <space>-<stuff>\n, stopping when one of them has an extra \n at the
    > end.
    >
    > Try this and see if it's close to what you need:
    >
    > /(^\ *-.*\n)+\n/


    /(^\ *-.*\n)+\ *\n/

    Perfect. A "many hairs" thanks! :)

    T.
    trans. (T. Onoma), Sep 22, 2004
    #6
  7. trans.  (T. Onoma)

    Pit Capitain Guest

    trans. (T. Onoma) schrieb:
    > Right now I'm working on this:
    >
    > /^[ ]*[-](.*\n)+.*\n/
    >
    > Applied to:
    >
    > %Q{
    > - xyz
    > - abc
    >
    > - not this
    > }
    >
    > I want it to match just the first section, essentially
    >
    > " - xyz\n - abc\n\n".
    >
    > But it's matching the whole string. Can any of you Regexp experts fill me in?


    The repeating group in your regexp is (.*\n)+ and this matches every line. You
    have to enlarge this group as in /^( *-.*\n)+.*\n/

    HTH

    Pit
    Pit Capitain, Sep 22, 2004
    #7
  8. On Wednesday 22 September 2004 03:55 pm, Pit Capitain wrote:
    > The repeating group in your regexp is (.*\n)+ and this matches every line.
    > You have to enlarge this group as in /^( *-.*\n)+.*\n/


    That makes sense. Thanks all!

    T.
    trans. (T. Onoma), Sep 23, 2004
    #8
    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. EXI-Andrews, Jack

    re.match -- not greedy?

    EXI-Andrews, Jack, Nov 19, 2006, in forum: Python
    Replies:
    6
    Views:
    469
  2. EXI-Andrews, Jack

    re.match -- not greedy?

    EXI-Andrews, Jack, Nov 22, 2006, in forum: Python
    Replies:
    0
    Views:
    310
    EXI-Andrews, Jack
    Nov 22, 2006
  3. CarlosRivera
    Replies:
    2
    Views:
    87
    CarlosRivera
    Sep 21, 2004
  4. Dan Kelly

    Greedy and non greedy quantifiers

    Dan Kelly, Jan 17, 2008, in forum: Ruby
    Replies:
    4
    Views:
    137
    Robert Klemme
    Jan 19, 2008
  5. Matt Garrish

    greedy v. non-greedy matching

    Matt Garrish, Feb 16, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    155
    Matt Garrish
    Feb 16, 2004
Loading...

Share This Page