正则表达式匹配成功,如何在遍历所有捕获组时取得组名?

橡木疙瘩 2013-06-28 09:30:23
有这样一段代码:

Regex testXpress = new Regex(
@"^\s*(?:" +
@"(?:" +
@"(?:(?<low_bound>\d+(?:\.\d+)?))" +
@"\s*(?:-|~)\s*" +
@"(?:(?<high_bound>\d+(?:\.\d+)?))" +
")" +//0.78 - 1.23; 1-2.24; 4.3 -6; 5-9
@"|(?:(?:≤|(?:<=))\s*(?<high_bound>\d+(?:\.\d+)?))" + //<= 1.23; ≤1.12; <=1; ≤ 2
@"|(?:(?:≥|(?:>=))\s*(?<low_bound>\d+(?:\.\d+)?))" + //>= 1.23; ≥1.12; >=1; ≥ 2
@"|(?:(?:<|<)\s*(?<high_bound>\d+(?:\.\d+)?))" + //< 1.23; <1.12; <1; < 2
@"|(?:(?:>|>)\s*(?<low_bound>\d+(?:\.\d+)?))" + //> 1.23; >1.12; >1; > 2
@"|(?<accurate>\d+(?:\.\d+)?)" + // 12.45; 36
@")\s*$"
);

Match mc = testXpress.Match(textBox1.Text);

if (mc.Success)
{
foreach (Group gp in mc.Groups)
{
//string name = ?这里怎么获得捕获组的名字》?
foreach (Capture cp in gp.Captures)
{
//do something with name and cp.value
}
}
}

正则表达式匹配成功后,要遍历gropus中的group,并根据组名对捕获结果进行处理。
上面给出的正则表达式只是其中最简单的一个。

...全文
410 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
橡木疙瘩 2013-06-28
  • 打赏
  • 举报
回复
多谢。只在结果中找了,没去想过在表达式里找。
Anod 2013-06-28
  • 打赏
  • 举报
回复
引用 4 楼 q107770540 的回复:
[quote=引用 3 楼 u010936098 的回复:] [quote=引用 2 楼 Chinajiyong 的回复:] Regex testXpress = new Regex("(?<dd>)[\u4e00-\u9fa5]+"); Match mc = testXpress.Match("dsafdsa真大事fdsfgdsag国防生23423"); string xx = mc.Groups["dd"].Value; xx就是对应捕获组dd所捕获的值啊
这我知道。 我不是要按名字去访问,而是要遍历所有的捕获组,然后根据名字去处理值。 问题的关键在于,事先不知道有哪些命名捕获组,否则我就可以把组名写成一个数组去遍历了。[/quote] it is also easy:
GroupCollection groups = regex.Match(line).Groups;

foreach (string groupName in regex.GetGroupNames())
{
    Console.WriteLine(
       "Group: {0}, Value: {1}",
       groupName,
       groups[groupName].Value);
}
[/quote] 严重同意
q107770540 2013-06-28
  • 打赏
  • 举报
回复
引用 3 楼 u010936098 的回复:
[quote=引用 2 楼 Chinajiyong 的回复:] Regex testXpress = new Regex("(?<dd>)[\u4e00-\u9fa5]+"); Match mc = testXpress.Match("dsafdsa真大事fdsfgdsag国防生23423"); string xx = mc.Groups["dd"].Value; xx就是对应捕获组dd所捕获的值啊
这我知道。 我不是要按名字去访问,而是要遍历所有的捕获组,然后根据名字去处理值。 问题的关键在于,事先不知道有哪些命名捕获组,否则我就可以把组名写成一个数组去遍历了。[/quote] it is also easy:
GroupCollection groups = regex.Match(line).Groups;

foreach (string groupName in regex.GetGroupNames())
{
    Console.WriteLine(
       "Group: {0}, Value: {1}",
       groupName,
       groups[groupName].Value);
}
橡木疙瘩 2013-06-28
  • 打赏
  • 举报
回复
引用 2 楼 Chinajiyong 的回复:
Regex testXpress = new Regex("(?<dd>)[\u4e00-\u9fa5]+"); Match mc = testXpress.Match("dsafdsa真大事fdsfgdsag国防生23423"); string xx = mc.Groups["dd"].Value; xx就是对应捕获组dd所捕获的值啊
这我知道。 我不是要按名字去访问,而是要遍历所有的捕获组,然后根据名字去处理值。 问题的关键在于,事先不知道有哪些命名捕获组,否则我就可以把组名写成一个数组去遍历了。
EnForGrass 2013-06-28
  • 打赏
  • 举报
回复
Regex testXpress = new Regex("(?<dd>)[\u4e00-\u9fa5]+"); Match mc = testXpress.Match("dsafdsa真大事fdsfgdsag国防生23423"); string xx = mc.Groups["dd"].Value; xx就是对应捕获组dd所捕获的值啊
q107770540 2013-06-28
  • 打赏
  • 举报
回复
if (mc.Success)
            {
   
                    foreach (Capture cp in mc.Groups["low_bound"].Captures)
                    {
                        //do something with name and cp.value
                    }
                
             }

111,095

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧