Eyal Oren wrote:
> Hi,
> 
> I am parsing query expressions, using a regular expression with
> multiple matches in it, e.g. /(\w+):(\w+)/.
> 
> I would like some code to execute on the first match (e.g.
> constructing some object out of it) and some other code on the second
> match (e.g. constructing some other object).
> 
> I can of course check the array of matches and find the non-nil
> element, and decide which code to execute. But that becomes very
> cumbersome with a large regex (with say 10 different matches).
> 
> So I would rather like to attach some code in a match directly, as one
> does in parsing generators, e.g.
> /(\w+:do_method):(\w+:do_other_method)/.
> 
> Would something like that be possible in Ruby? I tried searching but
> I'm not sure how such a feature would be called.

No, I don't think it's possible.   You can do this

string.scan(/(\w+):(\w+)/) do |match|
  case match.inject(1) {|pos,x| break pos if x;pos + 1}
    when 1
      # code for group 1
    when 2
      # ...
  end
end

Kind regards

    robert