On Fri, Mar 25, 2011 at 9:14 AM, Robert Klemme
<shortcutter / googlemail.com> wrote:
> On Fri, Mar 25, 2011 at 2:58 AM, 7stud -- <bbxx789_05ss / yahoo.com> wrote:
>> perl's regexes have a recursive feature, which can solve your problem.
>> Want to call an external script?
>
> Not necessary: Ruby has it, too.
>
> irb(main):001:0> s="x(a(b)()c)y"
> => "x(a(b)()c)y"
> irb(main):002:0> %r{(?<ne> \( (?:\g<ne>|[^()]+)* \) )}x.match s
> => #<MatchData "(a(b)()c)" ne:"(a(b)()c)">
>
> irb(main):003:0> s = "f_(a)(x)=x^(2)+(5x//f'(x))"
> => "f_(a)(x)=x^(2)+(5x//f'(x))"
> irb(main):004:0> s.scan %r{(?<ne> \( (?: \g<ne> | [^()] )* \) )}x
> => [["(a)"], ["(x)"], ["(2)"], ["(5x//f'(x))"]]
>
> See http://www.geocities.jp/kosako3/oniguruma/doc/RE.txt

PS: Here's a more targeted one

irb(main):054:0> %r{\( (?<ne> [^()] | \( \g<ne> \) )* // \g<ne>* \)}x.match s
=> #<MatchData "(5x//f'(x))" ne:"(x)">

:-)

Cheers

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/