What branch of (|) RE is match?

S

S.Z.

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?
 
J

Jeff Wood

------=_Part_10454_13750018.1133011379378
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

a =3D "hello world"
b =3D /(ell)|(goo)/
c =3D 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.

I have a fragment like this:

variant=3D [ /[abc]/, /[123]/, /[xyz]/ ];
re=3D /aaa\s(#{variant.join('|')})\sbbb/;
mch=3D 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=3D 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

------=_Part_10454_13750018.1133011379378--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top