[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Home > Backend Development > C++ > How to Access Named Capturing Groups in .NET Regex?

How to Access Named Capturing Groups in .NET Regex?

Barbara Streisand
Release: 2025-01-12 09:27:43
Original
136 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template