Ross Bamford
12/14/2005 11:51:00 PM
On Wed, 14 Dec 2005 21:59:27 -0000, ako... <akonsu@gmail.com> wrote:
> thank you. the question is general.
>
> if i wanted to parse a list of letters separated by spaces and commas:
>
> 'a , b,c' =~ /^(?:(\w)\s*,\s*)*(\w)$/
>
> i need to get ['a','b'] in group 1 and ['c'] in group 2. yes, i know i
> can split, then massage the result some more and get the final result.
> is there a way to get to groups' captures after a regex match? like in
> microsoft's .net?
>
I don't really get what you mean. I don't understand the rules that got a
and b into one group and c into another. When you say it's a general
question, do you mean you just want access to the captures from some
regexp match?
irb(main):009:0> "a , b,c" =~ /(\w\s*?,\s*?\w)\s*?,\s*?(\w)/
=> 0
irb(main):010:0> $1
=> "a , b"
irb(main):011:0> $2
=> "c"
irb(main):012:0> $~[1]
=> "a , b"
irb(main):013:0> $~[2]
=> "c"
irb(main):014:0> md = /(\w\s*?,\s*?\w)\s*?,\s*?(\w)/.match("a, b,c")
=> #<MatchData:0xb7a47860>
irb(main):015:0> md[1]
=> "a, b"
irb(main):016:0> md.captures[1]
=> "c"
irb(main):017:0> $~.inspect
=> "#<MatchData:0xb7a47860>"
(and others...)
Hope that helps,
Ross
--
Ross Bamford - rosco@roscopeco.remove.co.uk
"\e[1;31mL"