Question about perlreref - are {n} and {n}? different?

Discussion in 'Perl Misc' started by usenet@DavidFilmer.com, Oct 26, 2005.

  1. Guest

    perlreref::QUANTIFIERS says:

    Quantifiers are greedy by default -- match the longest leftmost.
    Maximal Minimal Allowed range
    ------- ------- -------------
    {n,m} {n,m}? Must occur at least n times but no more than m times
    {n,} {n,}? Must occur at least n times
    {n} {n}? Must occur exactly n times
    [etc, snip]

    Aren't {n} and {n}? really the same thing? You can't have greediness
    if you stipulate an exact count, so you can't negate greediness if you
    can't _have_ greediness, so {n}? (though it may be syntically correct)
    makes no sense to me.
     
    , Oct 26, 2005
    #1
    1. Advertising

  2. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > perlreref::QUANTIFIERS says:
    >
    > Quantifiers are greedy by default -- match the longest leftmost.
    > Maximal Minimal Allowed range
    > ------- ------- -------------
    > {n,m} {n,m}? Must occur at least n times but no more than m times
    > {n,} {n,}? Must occur at least n times
    > {n} {n}? Must occur exactly n times
    > [etc, snip]
    >
    > Aren't {n} and {n}? really the same thing? You can't have greediness
    > if you stipulate an exact count, so you can't negate greediness if you
    > can't _have_ greediness, so {n}? (though it may be syntically correct)
    > makes no sense to me.


    Yes, it's a degenerate case, the "?" has no effect. No big deal.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Oct 26, 2005
    #2
    1. Advertising

  3. Dr.Ruud Guest

    schreef:
    > perlreref::QUANTIFIERS says:
    >
    > Quantifiers are greedy by default -- match the longest leftmost.
    > Maximal Minimal Allowed range
    > ------- ------- -------------
    > {n,m} {n,m}? Must occur at least n times but no more than m times



    The 'Must occur ... no more than m times' is not accurate.

    #!/usr/bin/perl -w
    use strict;

    my $s = 'a'x100; # is more than 50 times

    sub run {
    local ($,, $\) = (' ', "\n");
    my $re; ($re, $_) = @_;
    s/$re/$1/;
    print length, length($1);
    }

    run 'a{10,50}?(.*)' , $s;
    run 'a{10,50}?(.*?)a', $s;
    run 'a{10,50}?(.*?)' , $s;
    run 'a{10,50}(.*?)' , $s;
    run 'a{10,50}(.*)' , $s;

    output:
    90 90
    89 0
    90 0
    50 0
    50 50

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Oct 26, 2005
    #3
  4. Guest

    Dr.Ruud wrote:
    > The 'Must occur ... no more than m times' is not accurate.
    > [code snip]
    > output:
    > 90 90
    > 89 0
    > 90 0
    > 50 0
    > 50 50


    I'm confused - the output seems consistent with the perlreref
    statement. Can you explain which one(s) of these outputs you feel is
    inconsistent, and what you would have expected it to be?
     
    , Oct 26, 2005
    #4
  5. Dr.Ruud Guest

    schreef:
    > Dr.Ruud:


    >> The 'Must occur ... no more than m times' is not accurate.

    >
    > I'm confused - the output seems consistent with the perlreref
    > statement. Can you explain which one(s) of these outputs you feel is
    > inconsistent, and what you would have expected it to be?


    I did not expect any of the outputs to be different.

    It's just nitpicking. The description says that if m=50, my 'a' must not
    occur more than 50 times, so I gave it 100 'a'-s.

    "Must occur at least n times but no more than m times" -->
    "Must match at least n times, and will try to match up to m times".

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Oct 27, 2005
    #5
  6. Juha Laiho Guest

    "Dr.Ruud" <> said:
    > schreef:
    >> perlreref::QUANTIFIERS says:
    >>
    >> Quantifiers are greedy by default -- match the longest leftmost.
    >> Maximal Minimal Allowed range
    >> ------- ------- -------------
    >> {n,m} {n,m}? Must occur at least n times but no more than m times

    >
    >
    >The 'Must occur ... no more than m times' is not accurate.


    Hmm.. given a little bit more context, it is accurate:

    AX{n,m}B

    Here, to make the whole RE match, you must have an A, followed by at least
    n, but no more than m Xs, followed by a B.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
     
    Juha Laiho, Oct 29, 2005
    #6
  7. Dr.Ruud Guest

    Juha Laiho schreef:
    > Dr.Ruud:


    >>> perlreref::QUANTIFIERS says:
    >>>
    >>> Quantifiers are greedy by default -- match the longest leftmost.
    >>> Maximal Minimal Allowed range
    >>> ------- ------- -------------
    >>> {n,m} {n,m}? Must occur at least n times but no more than m
    >>> times

    >>
    >> The 'Must occur ... no more than m times' is not accurate.

    >
    > Hmm.. given a little bit more context, it is accurate:
    >
    > AX{n,m}B


    Yes, but not in general.

    echo 'AXXXXXXXXB' |
    perl -ne 'chomp; print "$_:OK\n" if /AX{2,3}/;'


    Compare perlre(1)

    {n} Match exactly n times
    {n,} Match at least n times
    {n,m} Match at least n but not more than m times

    Compare grep(1)

    {n} The preceding item is matched exactly n times.
    {n,} The preceding item is matched n or more times.

    {n,m} The preceding item is matched at least n times,
    but not more than m times.

    So perlreref(1) could use one of those.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Oct 29, 2005
    #7
  8. Joe Smith Guest

    Dr.Ruud wrote:
    > schreef:
    >
    >>perlreref::QUANTIFIERS says:
    >>
    >>Quantifiers are greedy by default -- match the longest leftmost.
    >> Maximal Minimal Allowed range
    >> ------- ------- -------------
    >> {n,m} {n,m}? Must occur at least n times but no more than m times

    >
    > The 'Must occur ... no more than m times' is not accurate.


    It is accurate, when you realize that it is talking about the
    characters that were actually part of the matched string.
    Characters outside the matched string are irrelevant when
    the match succeeds.

    > #!/usr/bin/perl -w
    > use strict;
    >
    > my $s = 'a'x100; # is more than 50 times
    >
    > sub run {
    > local ($,, $\) = (' ', "\n");
    > my $re; ($re, $_) = @_;
    > s/$re/$1/;
    > print length, length($1);
    > }
    >
    > run 'a{10,50}?(.*)' , $s;
    > run 'a{10,50}?(.*?)a', $s;
    > run 'a{10,50}?(.*?)' , $s;
    > run 'a{10,50}(.*?)' , $s;
    > run 'a{10,50}(.*)' , $s;
    >
    > output:
    > 90 90
    > 89 0
    > 90 0
    > 50 0
    > 50 50


    run 'a{10,50}?(.*)' , $s;
    First part matches minimum; 'a'x10.
    Second part matches rest of string; 'a'x90.
    Replacing first+second with just second = 'a'x90.
    Expected result of "90 90" = yes.

    run 'a{10,50}?(.*?)a', $s;
    First part matches minimum; 'a'x10.
    Second part matches the null string.
    Third part matches 11th a.
    Replacing first+second+third with just second leaves the
    89 characters that were not part of the overall match = 'a'x89.
    Expected result of "89 0" = yes.

    run 'a{10,50}?(.*?)' , $s;
    First part matches minimum; 'a'x10.
    Second part matches the null string.
    Replacing first+second with just second leaves the
    90 characters that were not part of the overall match = 'a'x90.
    Expected result of "90 0" = yes.

    run 'a{10,50}(.*?)' , $s;
    First part matches maximum; 'a'x50.
    Second part matches the null string.
    Replacing first+second with just second leaves the
    50 characters that were not part of the overall match = 'a'x50.
    Expected result of "50 0" = yes.

    run 'a{10,50}(.*)' , $s;
    First part matches maximum; 'a'x50.
    Second part matches rest of string; 'a'x50.
    Replacing first+second with just second = 'a'x50.
    Expected result of "50 50" = yes.

    The s/$re/$1/ just confuses things. This is better:

    #!/usr/bin/perl -w
    use strict;

    my $s = 'a'x100; # is more than 50 times

    sub run {
    my $re; ($re, $_) = @_;
    /$re/;
    print "\$1='$1' \$2='$2' rest=|$'|\n";
    }

    run '(a{10,50}?)(.*)' , $s;
    run '(a{10,50}?)(.*?)a', $s;
    run '(a{10,50}?)(.*?)' , $s;
    run '(a{10,50})(.*?)' , $s;
    run '(a{10,50})(.*)' , $s;
    $1='aaaaaaaaaa'
    $2='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    rest=||
    $1='aaaaaaaaaa' $2=''
    rest=|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
    $1='aaaaaaaaaa' $2=''
    rest=|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
    $1='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' $2=''
    rest=|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
    $1='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    $2='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' rest=||

    This shows that /a{10,50}?/ matches the first 10 characters of the
    string and /a{10,50}/ matches the first 50 characters of the string.

    -Joe
     
    Joe Smith, Oct 31, 2005
    #8
  9. Dr.Ruud Guest

    'Must occur' -> 'Match' (was: Re: Question about perlreref - are {n} and {n}? different?)

    Joe Smith:
    > Dr.Ruud:


    >> The 'Must occur ... no more than m times' is not accurate.

    >
    > It is accurate, when you realize that it is talking about the
    > characters that were actually part of the matched string.


    s/were/are/

    The language is simply part of the non-accuracy. Both perlre and grep
    describe it accurate, no reason for perlreref to do otherwise.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Oct 31, 2005
    #9
    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. Tigerhillside
    Replies:
    3
    Views:
    497
    Joe Smith
    Feb 23, 2004
  2. AD
    Replies:
    1
    Views:
    1,050
    Francis Shanahan
    Aug 23, 2003
  3. Dotnetjunky
    Replies:
    0
    Views:
    387
    Dotnetjunky
    Aug 25, 2003
  4. Kevin Spencer
    Replies:
    0
    Views:
    428
    Kevin Spencer
    Aug 25, 2003
  5. chanmmn
    Replies:
    2
    Views:
    330
    Gawel
    Sep 7, 2004
Loading...

Share This Page