Regexp: non greedy?

Discussion in 'Perl Misc' started by Oliver, May 20, 2008.

  1. Oliver

    Oliver Guest

    Dear all

    I know, that its probably a little boring for the "pro"s to answer
    question about regexp...
    ....but, bevor I start to cry I'd like to ask for a helping hand.

    I have the following regexp:
    $message =~ m/(\:59\:(.+)?(\n\:\d\d\:))/s ;

    I am expecting it to grab everything whats between :59: and :70: into
    $2.
    Unfortunately it does it greedy that for some examples it does not
    the add the first but the last match of :59: and :70:.

    What can I do, that it matches the first occurance between :59: and :
    70: ?

    Regards,
    Oliver

    Example Data:

    a)

    :21: Hello World
    :23A: 12344523423
    :59: I Am a line
    I am another line
    me too
    I could be another but mustn't
    :70: ABRV
    :71A: kkk

    b)

    :21: Hello World
    :23A: 12344523423
    :59: I Am a line
    I am another line with a number
    :70: ABRV
    :71G: kkk
    :70: ABRV2
    :71H: lll
    :70: ABRV3
     
    Oliver, May 20, 2008
    #1
    1. Advertising

  2. Oliver wrote:

    > I have the following regexp:
    > $message =~ m/(\:59\:(.+)?(\n\:\d\d\:))/s ;
    >
    > I am expecting it to grab everything whats between :59: and :70: into
    > $2.
    > Unfortunately it does it greedy that for some examples it does not
    > the add the first but the last match of :59: and :70:.
    >
    > What can I do, that it matches the first occurance between :59: and :
    > 70: ?
    >
    > Regards,
    > Oliver
    >
    > Example Data:
    >
    > [snip, see below]


    #!/usr/bin/perl
    use strict;
    use warnings;

    my $x=<<END;
    :21: Hello World
    :23A: 12344523423
    :59: I Am a line
    I am another line
    me too
    I could be another but mustn't
    :70: ABRV
    :71A: kkk
    END

    my $y=<<END;
    :21: Hello World
    :23A: 12344523423
    :59: I Am a line
    I am another line with a number
    :70: ABRV
    :71G: kkk
    :70: ABRV2
    :71H: lll
    :70: ABRV3
    END

    for ($x,$y) {
    if (/:59:(.+?):70:/s) {
    print "\n'$1'\n";
    }
    }


    --
    RGB
     
    RedGrittyBrick, May 20, 2008
    #2
    1. Advertising

  3. Oliver

    Guest

    Oliver <> wrote:
    > Dear all
    >
    > I know, that its probably a little boring for the "pro"s to answer
    > question about regexp...
    > ...but, bevor I start to cry I'd like to ask for a helping hand.
    >
    > I have the following regexp:
    > $message =~ m/(\:59\:(.+)?(\n\:\d\d\:))/s ;


    The ? above is the "1 or 0 quantifier", not the greedy modifier, as it does
    not immediately follow another quantifier. It is quantifying the entire
    (.+) expression. To get what you want, you need to move ? inside the
    parenthesis.

    (.+?)

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , May 20, 2008
    #3
    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. Sam Pointon

    regexp non-greedy matching bug?

    Sam Pointon, Dec 4, 2005, in forum: Python
    Replies:
    8
    Views:
    367
    Fredrik Lundh
    Dec 5, 2005
  2. Tim Peters

    Re: regexp non-greedy matching bug?

    Tim Peters, Dec 4, 2005, in forum: Python
    Replies:
    0
    Views:
    391
    Tim Peters
    Dec 4, 2005
  3. John Hazen

    Re: regexp non-greedy matching bug?

    John Hazen, Dec 4, 2005, in forum: Python
    Replies:
    0
    Views:
    399
    John Hazen
    Dec 4, 2005
  4. Dan Kelly

    Greedy and non greedy quantifiers

    Dan Kelly, Jan 17, 2008, in forum: Ruby
    Replies:
    4
    Views:
    147
    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:
    164
    Matt Garrish
    Feb 16, 2004
Loading...

Share This Page