From: William James Date: 2007-08-21T06:34:58+09:00 Subject: Re: Regexp: named captures On Aug 20, 2:44 pm, Ari Brown wrote: > How do named captures in Ruby work? This is what I've tried: > > irb(main):001:0> if /(?.+)/ =~ /ari/ > irb(main):002:1> puts $name > irb(main):003:1> end > SyntaxError: compile error > (irb):1: undefined (?...) sequence: /(?.+)/ > from (irb):3 > > and if I do this: > > irb(main):007:0> if /(.+)/ =~ /ari/ > irb(main):008:1> puts $name > irb(main):009:1> end > TypeError: can't convert Regexp into String > from (irb):7 > > I get a Regexp => String error. > > Bwah? > aRi > -------------------------------------------| > Nietzsche is my copilot A work-around: md = "foo bar".match( /(\w+) (\w+)/ ) ==># name, surname = md.captures ==>["foo", "bar"] name ==>"foo" surname ==>"bar"