Jesús Gabriel y Galán
10/8/2007 12:59:00 PM
On 10/8/07, Max Williams <toastkid.williams@gmail.com> wrote:
> Jesús Gabriel y Galán wrote:
> > On 10/8/07, Max Williams <toastkid.williams@gmail.com> 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/)
=> #<MatchData:0xb7c97a04>
irb(main):002:0> a.to_a
=> ["1234", "1", "2"]
irb(main):003:0> a.captures
=> ["1", "2"]
Jesus.