Forum: Ruby What branch of (|) RE is match?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
599efad32cb0e6dea3a4db4de8673f5d?d=identicon&s=25 zbl (Guest)
on 2005-11-26 14:08
(Received via mailing list)
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?
A7c9c275318af9e1e3812fab9660cd7c?d=identicon&s=25 jeff.darklight (Guest)
on 2005-11-26 14:25
(Received via mailing list)
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. <zbl@operamail.com> 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 Wood
This topic is locked and can not be replied to.