# What branch of (|) RE is match?

**URL:** <https://www.ruby-forum.com/t/what-branch-of-re-is-match/48899>\
**Category:** Ruby\
**Created:** [November 26, 2005, 2:08pm UTC](https://www.ruby-forum.com/t/what-branch-of-re-is-match/48899 "2005-11-26T14:08:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zbl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b4bc9f/32.png) [@zbl](https://www.ruby-forum.com/u/zbl)\
**Post date:** [November 26, 2005, 2:08pm UTC](https://www.ruby-forum.com/t/what-branch-of-re-is-match/48899/1 "2005-11-26T14:08:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![zbl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b4bc9f/32.png) [@zbl](https://www.ruby-forum.com/u/zbl)\
**Post date:** [November 26, 2005, 2:25pm UTC](https://www.ruby-forum.com/t/what-branch-of-re-is-match/48899/2 "2005-11-26T14:25:07Z")

</div>

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](mailto: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](http://ruby-lang.org)”

Jeff W.
