in reply to Parsing nested parentheses
One way using regular expressions:
my $expr = '((A,B),C,(D,E))'; print extract('C',$expr),"\n"; print extract('B',$expr),"\n"; print extract('D',$expr),"\n"; sub extract { my $char = shift; my ($str,$dup) = (shift) x 2; 1 while $dup =~ s/\([^()$char]*\)/'.' x length $&/e; $dup =~ m/(\([^()$char]*$char[^()]*\))/; return substr($str,$-[1],length $1); } __END__ # output: ((A,B),C,(D,E)) (A,B) (D,E)
But you probably want to build a small expression parser.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Parsing nested parentheses
by melora (Scribe) on Nov 18, 2003 at 18:28 UTC |