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

Re: Parsing nested parentheses

by welchavw (Pilgrim)
on Nov 19, 2003 at 17:53 UTC ( #308359=note: print w/replies, xml ) Need Help??


in reply to Parsing nested parentheses

This will not work if all tokens are not the same length, because of the reliance on index, which seems ok given the sample dataset provided. Also, its probably not the best approach, but I believe it works and I had fun producing it.

use strict; use warnings; my $val = 'X'; my $min_len_subgroup; sub chk { my $subgroup = shift; if(-1 != index($subgroup, $val)) { if (!defined $min_len_subgroup || length $subgroup < length $min_l +en_subgroup) { $min_len_subgroup = $subgroup; } } } our $re = qr{ ( \( (?: (?> [^()]+ ) | (??{ $re }) )* \) ) (?{ chk($1) }) }x; my @pats = ( "(1(2(X)))", "(1(2(X,a))) (3(X,e,f))", "(1(2(X,b,c))) (3(X,f))", "(1(2(X,a))) (X,b)", "(1(2(X,a))) (X,b) X", "(1(2(X,a))) (X,b) (X)", ); for my $pat (@pats) { undef $min_len_subgroup; if (() = ($pat =~ /$re/g)) { print "$min_len_subgroup\n"; } }
,welchavw

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://308359]
help
Chatterbox?
LanX new life as prawn ... hmmm
[Eily]: LanX that sounds fishy to be honest

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2017-06-29 12:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (662 votes). Check out past polls.