Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Regex ERROR!

by nofernandes (Beadle)
on Jul 15, 2003 at 09:41 UTC ( [id://274330]=perlquestion: print w/replies, xml ) Need Help??

nofernandes has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code!!
foreach $line (@fich){ $i++; foreach $comm (@com){ if(($line eq $comm)||($line=~/$comm/)){ print"Line $i: $comm"; } } }

The problem is that the instruction in the if condition ($line=~/$comm/) gives me an error!!! "Nested quantifiers before HERE mark in regex m//** << HERE"....

The @comm and @fich arrays contain the contents of two distinct files!

Can someone tell me why this code gives me this error!?

Thank you all

Replies are listed 'Best First'.
Re: Regex ERROR!
by broquaint (Abbot) on Jul 15, 2003 at 09:44 UTC
    You'll need to escape the meta-characters in $comm e.g
    ## note the \Q before $comm if(($line eq $comm) || ($line =~ /\Q$comm/)){ print"Line $i: $comm"; }
    And it looks like a simple index would be better suited in this situation since you're just testing to see whether $comm is in $line e.g
    if(($line eq $comm) || (index($line, $comm) > -1)){ print"Line $i: $comm"; }
    See. perlre and quotemeta for more info on escaping regex meta-characters and perldiag for a more detailed explanation of your error.
    HTH

    _________
    broquaint

Re: Regex ERROR!
by Skeeve (Parson) on Jul 15, 2003 at 09:47 UTC
    have you tried /\Q$comm/ ? I fear it's a problem with the content of $comm.
      Thank you Very much!!!
      In did the \Q was the problem!!!

      It might be a stupid question but what does the \Q does in this context??

      Thank you again for your help!!

      Has in my country they said "The one that doesn´t know is like the one that cannot see!!"

        from the perlre man page
            \Q    quote (disable) pattern metacharacters till \E
        
        look at this code:
        $comm = '(xyz'; $line = '123(xyz)456'; if ($line =~ /$comm/) { #error! same as $line =~ /(xyz/ which ha +s an unmatched '(' # do stuff } if ($line =~ /\Q$comm/) { #no error! same as $line =~ /\(xyz/ -- ' +(' is escaped # do stuff }

        --

        flounder

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://274330]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-23 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found