通过C#中的GroupCollection进行迭代

Sve*_*ven 8 c# collections loops

我目前正在尝试在C#中使用正则表达式:

Regex reg_gameinfo = new Regex(@"PokerStars Game #(?<HID>[0-9]+):\s+(?:HORSE)? \(?(?<GAME>Hold'em|Razz|7 Card Stud|Omaha|Omaha Hi/Lo|Badugi) (?<LIMIT>No Limit|Limit|Pot Limit),? \(?(?<CURRENCYSIGN>\$|)?(?<SB>[.0-9]+)/\$?(?<BB>[.0-9]+) (?<CURRENCY>.*)\) - (?<DATETIME>.*$)", RegexOptions.Multiline);
Match matchresults = reg_gameinfo.Match(rawtext);
Dictionary<string,string> gameinfo = new Dictionary<string,string>();
if (matchresults.Success)
{
     gameinfo.Add("HID", matchresults.Groups["HID"].Value);
     gameinfo.Add("GAME", matchresults.Groups["GAME"].Value);
     ...
}
Run Code Online (Sandbox Code Playgroud)

我可以遍历matchresult.GroupsGroupCollection并将键值对添加到我的gameinfo字典中吗?

Joh*_*bom 13

(请参阅此问题:正则表达式:获取C#中捕获的组的名称)

您可以使用GetGroupNames:

Regex reg_gameinfo = new Regex(@"PokerStars Game #(?<HID>[0-9]+):\s+(?:HORSE)? \(?(?<GAME>Hold'em|Razz|7 Card Stud|Omaha|Omaha Hi/Lo|Badugi) (?<LIMIT>No Limit|Limit|Pot Limit),? \(?(?<CURRENCYSIGN>\$|)?(?<SB>[.0-9]+)/\$?(?<BB>[.0-9]+) (?<CURRENCY>.*)\) - (?<DATETIME>.*$)", RegexOptions.Multiline);
Match matchresults = reg_gameinfo.Match(rawtext);
Dictionary<string,string> gameinfo = new Dictionary<string,string>();

if (matchresults.Success)
    foreach(string groupName in reg_gameinfo.GetGroupNames())
        gameinfo.Add(groupName, matchresults.Groups[groupName].Value);  
Run Code Online (Sandbox Code Playgroud)