BernieC has asked for the wisdom of the Perl Monks concerning the following question:
What I want is a subroutine that matches a set of nested parens. That is, what I'd like is:
and have $parens end up with "a a (a a)(a a)" and $rest be "(b b(b)b b)" I seem to recall doing something like this when I was an undergraduate and it involved something like counting parens [that is, count +1 for a ( and -1 for a ) and you've got the first balanced set when the count == 0. Is there a more elegant way to do it in Perl?$str = "(a a (a a)(a a))(b b(b)b b)" and have my ($parens, $rest) = match($str) ;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Matching nested parens
by haukex (Archbishop) on Oct 01, 2020 at 19:41 UTC | |
by BernieC (Pilgrim) on Oct 01, 2020 at 21:24 UTC | |
by BernieC (Pilgrim) on Oct 01, 2020 at 22:25 UTC | |
by AnomalousMonk (Archbishop) on Oct 01, 2020 at 23:24 UTC | |
by BernieC (Pilgrim) on Oct 06, 2020 at 15:29 UTC | |
|
Re: Matching nested parens
by perlfan (Parson) on Oct 06, 2020 at 16:50 UTC | |
by jcb (Parson) on Oct 06, 2020 at 23:39 UTC |