Home >Backend Development >C++ >How to Access Named Capturing Groups in .NET Regex?

How to Access Named Capturing Groups in .NET Regex?

Barbara Streisand
Barbara StreisandOriginal
2025-01-12 09:27:43139browse

How to Access Named Capturing Groups in .NET Regex?

Correctly Accessing Named Capture Groups in .NET Regular Expressions

Your original approach to extracting named capture groups from a .NET regular expression was slightly flawed. The correct method involves using the Groups collection of the Match object. Here's the adjusted code:

<code class="language-csharp">string page = Encoding.ASCII.GetString(bytePage);
Regex qariRegex = new Regex("<td><a href=\"(?<link>.*?)\">(?<name>.*?)</a></td>");
MatchCollection matches = qariRegex.Matches(page);

foreach (Match match in matches)
{
    MessageBox.Show(match.Groups["link"].Value);
    MessageBox.Show(match.Groups["name"].Value);
}</code>

This revised code iterates through the MatchCollection (containing all matches found in the input string). For each Match, it accesses the named capture groups ("link" and "name") via the Groups collection and displays their respective values using MessageBox.Show. This ensures accurate retrieval of the captured data.

The above is the detailed content of How to Access Named Capturing Groups in .NET Regex?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn