tags:

views:

165

answers:

2

Let's say I'm matching with a pattern that has subexpressions like this:

Regex myRegex = new Regex("(man|woman|couple) seeking (man|woman|couple|aardvark)");

string myStraightText = "Type is man seeking woman, age is 44";
MatchCollection myStraightMatches = myRegex.Matches(myStraightText);

string myGayText = "Type is man seeking man, age is 39";
MatchCollection myGayMatches = myRegex.Matches(myGayText);

string myBizarreText = "Type is couple seeking aardvark, age is N/A";
MatchCollection myBizarreMatches = myRegex.Matches(myBizarreText);

On the first match, I'd like to recover the information that the first subexpression matched "man" (and not "woman" or "couple") and the second subexpression matched "woman" (and not "man" or "couple" or "aardvark"). Whereas the second match it was "man" and "man" etc. Is this information available somewhere in the Match object?

I only know how to get the complete matched string. For instance,

foreach (Match myMatch in myStraightMatches)
{
    tbOutput.Text += String.Format("{0}\n", myMatch);
}

gets "man seeking woman". But I don't know which parts of that string came from which subexpression.

+4  A: 

Try this:

myMatch.Groups[0] // "man seeking woman"
myMatch.Groups[1] // "man"
myMatch.Groups[2] // "woman"

EDIT: To make answer more complete, if you have:

new Regex("(?<seeker>man|woman|couple) seeking (?<target>man|woman|couple)");

you can use:

myMatch.Groups["seeker"] // "man"
myMatch.Groups["target"] // "woman"
Rubens Farias
+1 for Groups. Remember you can name the Groups in .net, like (?<seeker>(man|woman|couple)), will let you identify the Group with myMatch.Groups["seeker"]
IanR
@IanR, ty; updated it
Rubens Farias
+3  A: 

You can use numbered groups as Rubens Farias suggested. However, numbered groups can often be fragile to small programmer mistakes or subsequent changes to the regex.

I generally try to used named groups. The syntax looks like (?<name>...)

Then, you can reference the groups like so:

myMatch.Groups["name"]
Kennet Belenky
+1, nice tip Kennet
Rubens Farias