如何访问一个名为捕获组在.NET正则表达式? [英] How do I access named capturing groups in a .NET Regex?

查看:145
本文介绍了如何访问一个名为捕获组在.NET正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很难找到,介绍如何使用命名捕获组在C#中一个很好的资源。这是code,我到目前为止有:

I'm having a hard time finding a good resource that explains how to use Named Capturing Groups in C#. This is the code that I have so far:

string page = Encoding.ASCII.GetString(bytePage);
Regex qariRegex = new Regex("<td><a href=\"(?<link>.*?)\">(?<name>.*?)</a></td>");
MatchCollection mc = qariRegex.Matches(page);
CaptureCollection cc = mc[0].Captures;
MessageBox.Show(cc[0].ToString());

然而,这永远只是展示了全系列:

However this always just shows the full line:

<td><a href="/path/to/file">Name of File</a></td>

我已经尝试了,我在不同的网站上找到了其他几个方法,但我一直得到同样的结果。

I have experimented with several other "methods" that I've found on various websites but I keep getting the same result.

我怎么能访问在我的正则表达式规定?命名的捕获组

How can I access the named capturing groups that are specified in my regex?

推荐答案

使用的匹配对象的集团回收,与捕获组的名称索引它,如

Use the group collection of the Match object, indexing it with the capturing group name, e.g.

foreach (Match m in mc){
    MessageBox.Show(m.Groups["link"]);
}

这篇关于如何访问一个名为捕获组在.NET正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆