On 10/8/07, Felix Windt <fwmailinglists / gmail.com> wrote:
> > From: Jes Gabriel y Gal [mailto:jgabrielygalan / gmail.com]
> > On 10/8/07, Max Williams <toastkid.williams / gmail.com> wrote:
> > > "3 * (1 + 2)" => ["1 + 2"]
> > > "3 * (1 + (4 / 2))" => ["1 + (4 / 2)"]
> > >
> > > can anyone show me how to do this?
> >
> > x = "3 * (1 + 2)".match(/\((.*)\)/)
> > x.captures
> > => ["1 + 2"]
> > x = "3 * (2 + (1 + 3))".match(/\((.*)\)/)
> > x.captures
> > => ["2 + (1 + 3)"]

> That can fail if you have more than one bracket pair on the lowest level:
>
> irb(main):002:0> "3 * (2 + (1 + 3)) + (1 * 4)".match(/\((.*)\)/).to_a
> => ["(2 + (1 + 3)) + (1 * 4)", "2 + (1 + 3)) + (1 * 4"]

True, what would be the expected result for this?

["2 + (1 + 3)", "1 * 4"] ???

I agree that for complex cases a regexp is not the solution. A
solution like yours counting parens (or with a stack) should be
preferred way.

Cheers,

Jesus.