Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help: Nested quantifiers in regex Problem

Reply
Thread Tools

Help: Nested quantifiers in regex Problem

 
 
Amy Lee
Guest
Posts: n/a
 
      10-24-2008
Hello,

I use qw to make a list with
my @sample = qw/"A..." "A..+" "A.+." "A+.." "A.++" "A+.+" "A++." "A+++"/;
Then there's a list called @ar and I want check whether the element at
@sample belongs to @ar. So I wrote the following codes
foreach my $item_sample (@sample)
{
unless (grep /$item_sample/, @ar) #line 38
{
push @ar, "$item_sample";
}
}
But wehn I run my script it shows following error messages
Nested quantifiers in regex; marked by <-- HERE in m/"A.++ <-- HERE "/ at ./x.pl line 38, <> line 44.

So I really feel very confused about it. Could you tell me why it happens
and how can I fix that?

Thank you very much.

Amy
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      10-24-2008
Amy Lee wrote:
>
> I use qw to make a list with
> my @sample = qw/"A..." "A..+" "A.+." "A+.." "A.++" "A+.+" "A++." "A+++"/;
> Then there's a list called @ar and I want check whether the element at
> @sample belongs to @ar. So I wrote the following codes
> foreach my $item_sample (@sample)
> {
> unless (grep /$item_sample/, @ar) #line 38
> {
> push @ar, "$item_sample";
> }
> }
> But wehn I run my script it shows following error messages
> Nested quantifiers in regex; marked by <-- HERE in m/"A.++ <-- HERE "/ at ./x.pl line 38, <> line 44.
>
> So I really feel very confused about it. Could you tell me why it happens
> and how can I fix that?


In a regular expression . matches any character except newline and .+
means to match any character one or more times. Trying to use .++ makes
no sense as you can't modify a modifier with +, only with ? so .+? would
be valid.




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
 
 
 
Martien Verbruggen
Guest
Posts: n/a
 
      10-24-2008
On Fri, 24 Oct 2008 12:50:23 +0800,
Amy Lee <(E-Mail Removed)> wrote:
> Hello,
>
> I use qw to make a list with
> my @sample = qw/"A..." "A..+" "A.+." "A+.." "A.++" "A+.+" "A++." "A+++"/;
> Then there's a list called @ar and I want check whether the element at
> @sample belongs to @ar. So I wrote the following codes
> foreach my $item_sample (@sample)
> {
> unless (grep /$item_sample/, @ar) #line 38
> {
> push @ar, "$item_sample";
> }
> }
> But wehn I run my script it shows following error messages
> Nested quantifiers in regex; marked by <-- HERE in m/"A.++ <-- HERE "/
> at ./x.pl line 38, <> line 44.
>
> So I really feel very confused about it. Could you tell me why it happens
> and how can I fix that?


In the above $item_sample is being treated as a regular expression. A
few of the things in @sample are simply not valid regular expressions. I
am assuming that you actually didn't mean them to be regular
expressions and that you wanted to check for string equality.

Also, are you aware that the strings you're looking at have double
quotes in them as well? Did you mean them to have those quotes in them?

There are a few ways in which you can fix this.

The minimal change is to correctly quote any metacharacters in the
regular expression (see the perlre documentation)

Change one line (your line 3:

unless (grep /\Q$item_sample/, @ar) #line 38

This does, however, probably not the right thing. It checks whether
$item_sample is PART of one of the elements of @ar. If you had "A+++"
and "A++" in that order in @sample, the second element would not end up
in @ar. I doubt that that was intentional.

You could anchor the regex at the start and end, but it would probably
be better to simply say what (I think) you mean, and test for equality:

unless (grep {$_ eq $item_sample} @ar) #line 38

This still is rather inefficient, because you have to loop through the
whole of @ar for every element of @sample. For small arrays that makes
no difference, but for larger arrays that is a problem.

I think that what you're trying to do is to assign the unique elements
of @sample to @ar, right? The canonical way to do that in Perl is by
using a hash:

my %seen;
my @ar = grep { ! $seen{$_}++ } @sample;

(Note that grep is used here only ones. These two lines are meant to
replace all the code you quoted above, not just line 38.)

Also check out the perlFAQ entry in perlfaq4 with the title "How can I
remove duplicate elements from a list or array?" This entry also
descibes this method with an explicit loop, instead of grep, which you
might find easier.

Martien
--
|
Martien Verbruggen | Quick! Hire a teenager while they still know
| everything.
|
 
Reply With Quote
 
Amy Lee
Guest
Posts: n/a
 
      10-24-2008
On Fri, 24 Oct 2008 17:51:14 +1100, Martien Verbruggen wrote:

> On Fri, 24 Oct 2008 12:50:23 +0800,
> Amy Lee <(E-Mail Removed)> wrote:
>> Hello,
>>
>> I use qw to make a list with
>> my @sample = qw/"A..." "A..+" "A.+." "A+.." "A.++" "A+.+" "A++." "A+++"/;
>> Then there's a list called @ar and I want check whether the element at
>> @sample belongs to @ar. So I wrote the following codes
>> foreach my $item_sample (@sample)
>> {
>> unless (grep /$item_sample/, @ar) #line 38
>> {
>> push @ar, "$item_sample";
>> }
>> }
>> But wehn I run my script it shows following error messages
>> Nested quantifiers in regex; marked by <-- HERE in m/"A.++ <-- HERE "/
>> at ./x.pl line 38, <> line 44.
>>
>> So I really feel very confused about it. Could you tell me why it happens
>> and how can I fix that?

>
> In the above $item_sample is being treated as a regular expression. A
> few of the things in @sample are simply not valid regular expressions. I
> am assuming that you actually didn't mean them to be regular
> expressions and that you wanted to check for string equality.
>
> Also, are you aware that the strings you're looking at have double
> quotes in them as well? Did you mean them to have those quotes in them?
>
> There are a few ways in which you can fix this.
>
> The minimal change is to correctly quote any metacharacters in the
> regular expression (see the perlre documentation)
>
> Change one line (your line 3:
>
> unless (grep /\Q$item_sample/, @ar) #line 38
>
> This does, however, probably not the right thing. It checks whether
> $item_sample is PART of one of the elements of @ar. If you had "A+++"
> and "A++" in that order in @sample, the second element would not end up
> in @ar. I doubt that that was intentional.
>
> You could anchor the regex at the start and end, but it would probably
> be better to simply say what (I think) you mean, and test for equality:
>
> unless (grep {$_ eq $item_sample} @ar) #line 38
>
> This still is rather inefficient, because you have to loop through the
> whole of @ar for every element of @sample. For small arrays that makes
> no difference, but for larger arrays that is a problem.
>
> I think that what you're trying to do is to assign the unique elements
> of @sample to @ar, right? The canonical way to do that in Perl is by
> using a hash:
>
> my %seen;
> my @ar = grep { ! $seen{$_}++ } @sample;
>
> (Note that grep is used here only ones. These two lines are meant to
> replace all the code you quoted above, not just line 38.)
>
> Also check out the perlFAQ entry in perlfaq4 with the title "How can I
> remove duplicate elements from a list or array?" This entry also
> descibes this method with an explicit loop, instead of grep, which you
> might find easier.
>
> Martien

Thank you guys, I use \Q to solve that. Really thank you very much.
 
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
Resolving Quantifiers of Sub-Expression Quantifiers: ie: /( ( ( foo\w* )+? | ( fle\w+)* ){0,5} ) )? bar/xg; ? sln@netherlands.com Perl Misc 0 02-18-2009 12:48 AM
JavaScript RegExp Quantifiers Nathan Sokalski ASP .Net 2 06-13-2008 07:51 AM
Question about Quantifiers in java Regular expression NeoGeoSNK Java 4 03-03-2008 09:49 PM
With regex, accessing multiple groups under quantifiers valan.wood@gmail.com Java 1 09-09-2007 10:26 PM
Regex: Backreferences do not work inside quantifiers? Wolfgang Thomas Perl Misc 16 03-09-2006 06:11 PM



Advertisments