From: Robert Klemme Date: 2011-03-25T17:28:56+09:00 Subject: Re: Regexp, matching only the content within parentheses On Fri, Mar 25, 2011 at 9:14 AM, Robert Klemme wrote: > On Fri, Mar 25, 2011 at 2:58 AM, 7stud -- 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{(? \( (?:\g|[^()]+)* \) )}x.match s > => # > > 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{(? \( (?: \g | [^()] )* \) )}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{\( (? [^()] | \( \g \) )* // \g* \)}x.match s => # :-) Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/