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

Re: Count capturing parentheses in a compiled regexp

by hv (Parson)
on May 02, 2004 at 12:12 UTC ( #349802=note: print w/ replies, xml ) Need Help??


in reply to Count capturing parentheses in a compiled regexp

Nice snippet, but a couple of problems: the outer lookahead needs to be //s, else eg:

qr{( x )}x;
will fail.

Also, this will find parens in embedded code and comments and treat as captures. If that doesn't seem worth worrying about it'd be enough to add a caveat I guess, else I think you can mimic perl's simplistic parsing reasonable easily for the code (just count to the balancing close-brace). Comments may actually be the trickiest, since you'll need to know when //x is in force:

qr{ (?x: # (comment) ) (?-x: # (capture) ) }

Oops, another one: parens in [ ... ] should be ignored too; I'm not sure how easy those would be to parse, since not every ] closes the selection.

Hugo


Comment on Re: Count capturing parentheses in a compiled regexp
Select or Download Code
Re: Re: Count capturing parentheses in a compiled regexp
by BooK (Curate) on May 02, 2004 at 15:20 UTC

    Thanks a lot for finding these shortcomings in my code. :-) I'll submit updated versions as I correct them.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://349802]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2015-07-07 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls