如何访问.NET Regex中的命名捕获组?

时间:2022-02-23 23:58:59

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:

我很难找到一个好的资源来解释如何在c#中使用命名捕获组。这是我到目前为止的代码:

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?

如何访问regex中指定的命名捕获组?

4 个解决方案

#1


231  

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

使用Match对象的组集合,用捕获的组名对其进行索引,例如。

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

#2


98  

You specify the named capture group string by passing it to the indexer of the Groups property of a resulting Match object.

通过将指定的捕获组字符串传递给结果匹配对象的组属性的索引器,可以指定命名的捕获组字符串。

Here is a small example:

这里有一个小例子:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        String sample = "hello-world-";
        Regex regex = new Regex("-(?<test>[^-]*)-");

        Match match = regex.Match(sample);

        if (match.Success)
        {
            Console.WriteLine(match.Groups["test"].Value);
        }
    }
}

#3


11  

The following code sample, will match the pattern even in case of space characters in between. i.e. :

下面的代码示例将匹配模式,即使中间有空格字符。例如:

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

as well as:

以及:

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

Method returns true or false, depending on whether the input htmlTd string matches the pattern or no. If it matches, the out params contain the link and name respectively.

方法返回true或false,这取决于输入htmlTd字符串是否与模式匹配。如果匹配,则out params分别包含链接和名称。

/// <summary>
/// Assigns proper values to link and name, if the htmlId matches the pattern
/// </summary>
/// <returns>true if success, false otherwise</returns>
public static bool TryGetHrefDetails(string htmlTd, out string link, out string name)
{
    link = null;
    name = null;

    string pattern = "<td>\\s*<a\\s*href\\s*=\\s*(?:\"(?<link>[^\"]*)\"|(?<link>\\S+))\\s*>(?<name>.*)\\s*</a>\\s*</td>";

    if (Regex.IsMatch(htmlTd, pattern))
    {
        Regex r = new Regex(pattern,  RegexOptions.IgnoreCase | RegexOptions.Compiled);
        link = r.Match(htmlTd).Result("${link}");
        name = r.Match(htmlTd).Result("${name}");
        return true;
    }
    else
        return false;
}

I have tested this and it works correctly.

我已经测试过这个,它工作得很好。

#4


0  

Additionally if someone have a use case where he needs group names before executing search on Regex object he can use:

此外,如果某人在对Regex对象执行搜索之前需要组名,那么他可以使用:

var regex = new Regex(pattern); // initialized somewhere
// ...
var groupNames = regex.GetGroupNames();

#1


231  

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

使用Match对象的组集合,用捕获的组名对其进行索引,例如。

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

#2


98  

You specify the named capture group string by passing it to the indexer of the Groups property of a resulting Match object.

通过将指定的捕获组字符串传递给结果匹配对象的组属性的索引器,可以指定命名的捕获组字符串。

Here is a small example:

这里有一个小例子:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        String sample = "hello-world-";
        Regex regex = new Regex("-(?<test>[^-]*)-");

        Match match = regex.Match(sample);

        if (match.Success)
        {
            Console.WriteLine(match.Groups["test"].Value);
        }
    }
}

#3


11  

The following code sample, will match the pattern even in case of space characters in between. i.e. :

下面的代码示例将匹配模式,即使中间有空格字符。例如:

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

as well as:

以及:

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

Method returns true or false, depending on whether the input htmlTd string matches the pattern or no. If it matches, the out params contain the link and name respectively.

方法返回true或false,这取决于输入htmlTd字符串是否与模式匹配。如果匹配,则out params分别包含链接和名称。

/// <summary>
/// Assigns proper values to link and name, if the htmlId matches the pattern
/// </summary>
/// <returns>true if success, false otherwise</returns>
public static bool TryGetHrefDetails(string htmlTd, out string link, out string name)
{
    link = null;
    name = null;

    string pattern = "<td>\\s*<a\\s*href\\s*=\\s*(?:\"(?<link>[^\"]*)\"|(?<link>\\S+))\\s*>(?<name>.*)\\s*</a>\\s*</td>";

    if (Regex.IsMatch(htmlTd, pattern))
    {
        Regex r = new Regex(pattern,  RegexOptions.IgnoreCase | RegexOptions.Compiled);
        link = r.Match(htmlTd).Result("${link}");
        name = r.Match(htmlTd).Result("${name}");
        return true;
    }
    else
        return false;
}

I have tested this and it works correctly.

我已经测试过这个,它工作得很好。

#4


0  

Additionally if someone have a use case where he needs group names before executing search on Regex object he can use:

此外,如果某人在对Regex对象执行搜索之前需要组名,那么他可以使用:

var regex = new Regex(pattern); // initialized somewhere
// ...
var groupNames = regex.GetGroupNames();