What branch of (|) RE is match?


#1

I have a fragment like this:

variant= [ /[abc]/, /[123]/, /[xyz]/ ];
re= /aaa\s(#{variant.join(’|’)})\sbbb/;
mch= re.match($stdin.read);

(Remove the semicolons if you hate them.)

Now, I need to know what branch of variant is match, /[abc]/ or /[123]/
or /[xyz]/
How can I reveal it?

variant can be iterated with mch[1] :

vm= variant.find { |v| v.match(mch[1]); }

But it is superfluous work, isn’t it?


#2

If you put each of the variants into seperate groups, you can check nil
on
groups and will know exactly which matched.

a = “hello world”
b = /(ell)|(goo)/
c = b.match a
c.to_a # first element is match, second the match on the group (ell),
third
the match on (goo)

should be what you’re after.

j.

On 11/26/05, S.Z. removed_email_address@domain.invalid wrote:

or /[xyz]/
How can I reveal it?

variant can be iterated with mch[1] :

vm= variant.find { |v| v.match(mch[1]); }

But it is superfluous work, isn’t it?


“Remember. Understand. Believe. Yield! -> http://ruby-lang.org

Jeff W.