From: "Jesús Gabriel y Galán" Date: 2007-10-08T22:03:44+09:00 Subject: Re: regexp question - look for parentheses then remove them On 10/8/07, Felix Windt wrote: > > From: Jesús Gabriel y Galán [mailto:jgabrielygalan@gmail.com] > > On 10/8/07, Max Williams 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.