Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

doubt on regex

by loki (Novice)
on Jan 25, 2010 at 12:58 UTC ( [id://819473]=perlquestion: print w/replies, xml ) Need Help??

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

^\s*[)]*\s*$

and

^\s*[(]*\s*$

matches the brackets `(` and `)` which commented as matched. That is, what am trying is to ignore brackets that are single and not (condition1) brackets.. i want to regex to match the brackets which are commented as not matched.. ie 1 or more brackets on the same line..

while ( #matches here ( #matches here (condition1) && (condition2) && condition3 ) || (#matches here (condition4) || condition5 && (condition6) ) #matches here ) #matches here

but if I have like this it does not match:

while (( #does not match here (condition1) && (condition2) && condition3 ) || ( (condition4) || condition5 && (condition6) ) ) #does not match here

or

while ((( #does not match here (condition1) && (condition2) && condition3 )) || (( #does not match here (condition4) || condition5 && (condition6) ) ) ) #does not match here

How can I match all the brackets that are single?

Replies are listed 'Best First'.
Re: doubt on regex
by Utilitarian (Vicar) on Jan 25, 2010 at 14:33 UTC
    The issue is how does a machine determine which parenthesis shouldn't be there?
    Consider the following code and adapt
    my $string="one two one two"; my @ones=$string=~m/\bone\b/g; my @twos=$string=~m/\btwo\b/g; print scalar(@ones) ,"\t\"ones\"\n",scalar (@twos),"\t\"twos\"\n"; print "Equal numbers\n" if (scalar (@ones)== scalar (@twos));'
    You can flag that there are unmatched braces on a line, but a human may be needed to determine which is the incorrect brace. Hope this helps

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: doubt on regex
by lidden (Curate) on Jan 25, 2010 at 13:07 UTC
Re: doubt on regex
by JavaFan (Canon) on Jan 25, 2010 at 13:48 UTC
    You've already mastered "zero-or-more". Are you sure you weren't able to find the syntax for "one-or-more" in the manual?

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found