Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Question about perlreref - are {n} and {n}? different?

Reply
Thread Tools

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

 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      10-26-2005
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.

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      10-26-2005
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      10-26-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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."
 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      10-26-2005
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?

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-26-2005
(E-Mail Removed) 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."

 
Reply With Quote
 
Juha Laiho
Guest
Posts: n/a
 
      10-29-2005
"Dr.Ruud" <(E-Mail Removed)> said:
>(E-Mail Removed) 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)
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-29-2005
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."
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      10-31-2005
Dr.Ruud wrote:
> (E-Mail Removed) 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='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
rest=||
$1='aaaaaaaaaa' $2=''
rest=|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
$1='aaaaaaaaaa' $2=''
rest=|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
$1='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaa' $2=''
rest=|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa|
$1='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaa'
$2='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaa' 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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-31-2005
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."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question about .h file and .cpp file,also compiler question key9 C++ 7 09-13-2006 06:45 PM
Thanks (and apologies) for your comments on "The Most Obvious Question You Are Ever Likely To See" (e.g. No-Brainer security question) Curious George Computer Security 1 03-14-2005 10:01 PM
Re: General USB Printer setup question, and situation-specific question AG A+ Certification 0 01-13-2005 11:13 PM
Three question which is not yet answered clearly and correct so far !! challenging question in xsl and also in xsl fo Philip Meyer XML 0 11-30-2003 04:42 PM
newbie delurking, a lighting question, and a pricing question verminiusrex Digital Photography 2 08-08-2003 10:10 PM



Advertisments