From: "Jesús Gabriel y Galán" Date: 2007-10-08T21:58:30+09:00 Subject: Re: regexp question - look for parentheses then remove them On 10/8/07, Max Williams wrote: > Jesús Gabriel y Galán wrote: > > On 10/8/07, Max Williams wrote: > >> This gives me eg > >> i want to keep them, eg > >> > >> "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)"] > > > > Hope this helps, > > > > Jesus. > > ah, "captures" - that's the same as MatchData#to_a, right? Perfect, Not exactly, because the MatchData#to_a returns as the first position of the array the string that matched, and then starting from x[1] the captured groups. MatchData#captures only contains the captures. See the difference: irb(main):001:0> a = "123456".match(/(.)(.)\d\d/) => # irb(main):002:0> a.to_a => ["1234", "1", "2"] irb(main):003:0> a.captures => ["1", "2"] Jesus.