开发者

How to obtain matched subexpression from a Regex match? (C#)

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 objec开发者_StackOverflow社区t?

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.


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"


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"]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜