in reply to Peeling the Peelings
This avoids loops (external to the regex at least). It might compare favorably if you remove the balance check code.
sub peeln { local $_ = shift; my $n = shift; my($o, $c) = (tr[(][], tr[)][]); warn 'Unbalanced parens' unless $o == $c; $n = $c if $n == -1 or $n > $c; m[^ (?: .*? \( .*?){$n} (.+) (?: .*? \) .*?){$n} $]x; $1; } my @tests = ( 'hello(what(is(this(all(about)))))', 'hello(what(is(this(all(about))))))', 'hello((what(is(this(all(about)))))', 'A(B(c) D(e) f(g(h(i))))', ); for my $test ( @tests ) { print "$test : $_ : ", peeln( $test, $_ ) for -1 .. 5; } __END__ P:\>270595 hello(what(is(this(all(about))))) : -1 : about hello(what(is(this(all(about))))) : 0 : hello(what(is(this(all(about)) +))) hello(what(is(this(all(about))))) : 1 : what(is(this(all(about)))) hello(what(is(this(all(about))))) : 2 : is(this(all(about))) hello(what(is(this(all(about))))) : 3 : this(all(about)) hello(what(is(this(all(about))))) : 4 : all(about) hello(what(is(this(all(about))))) : 5 : about Unbalanced parens at P:\270595.pl8 line 28. Use of uninitialized value in print at P:\270595.pl8 line 42. hello(what(is(this(all(about)))))) : -1 : Unbalanced parens at P:\270595.pl8 line 28. hello(what(is(this(all(about)))))) : 0 : hello(what(is(this(all(about) +))))) Unbalanced parens at P:\270595.pl8 line 28. hello(what(is(this(all(about)))))) : 1 : what(is(this(all(about))))) Unbalanced parens at P:\270595.pl8 line 28. hello(what(is(this(all(about)))))) : 2 : is(this(all(about)))) Unbalanced parens at P:\270595.pl8 line 28. hello(what(is(this(all(about)))))) : 3 : this(all(about))) Unbalanced parens at P:\270595.pl8 line 28. hello(what(is(this(all(about)))))) : 4 : all(about)) Unbalanced parens at P:\270595.pl8 line 28. hello(what(is(this(all(about)))))) : 5 : about) Unbalanced parens at P:\270595.pl8 line 28. hello((what(is(this(all(about))))) : -1 : all(about Unbalanced parens at P:\270595.pl8 line 28. hello((what(is(this(all(about))))) : 0 : hello((what(is(this(all(about +))))) Unbalanced parens at P:\270595.pl8 line 28. hello((what(is(this(all(about))))) : 1 : (what(is(this(all(about)))) Unbalanced parens at P:\270595.pl8 line 28. hello((what(is(this(all(about))))) : 2 : what(is(this(all(about))) Unbalanced parens at P:\270595.pl8 line 28. hello((what(is(this(all(about))))) : 3 : is(this(all(about)) Unbalanced parens at P:\270595.pl8 line 28. hello((what(is(this(all(about))))) : 4 : this(all(about) Unbalanced parens at P:\270595.pl8 line 28. hello((what(is(this(all(about))))) : 5 : all(about Use of uninitialized value in print at P:\270595.pl8 line 42. A(B(c) D(e) f(g(h(i)))) : -1 : A(B(c) D(e) f(g(h(i)))) : 0 : A(B(c) D(e) f(g(h(i)))) A(B(c) D(e) f(g(h(i)))) : 1 : B(c) D(e) f(g(h(i))) A(B(c) D(e) f(g(h(i)))) : 2 : c) D(e) f(g(h(i)) A(B(c) D(e) f(g(h(i)))) : 3 : e) f(g(h(i) A(B(c) D(e) f(g(h(i)))) : 4 : g(h(i Use of uninitialized value in print at P:\270595.pl8 line 42. A(B(c) D(e) f(g(h(i)))) : 5 :
It doesn't handle (?term?) nesting like this a(b(c) d(e)), but it was unclear to me what the return would be in these circumstances?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Peeling the Peelings
by bobn (Chaplain) on Jul 02, 2003 at 01:33 UTC | |
by BrowserUk (Patriarch) on Jul 02, 2003 at 11:33 UTC | |
by BrowserUk (Patriarch) on Jul 02, 2003 at 10:11 UTC | |
by bobn (Chaplain) on Jul 03, 2003 at 13:18 UTC | |
by BrowserUk (Patriarch) on Jul 03, 2003 at 13:28 UTC | |
by bobn (Chaplain) on Jul 03, 2003 at 23:17 UTC | |
by BrowserUk (Patriarch) on Jul 02, 2003 at 09:16 UTC |