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

How Do I Access Named Capturing Groups in .NET Regex Matches?

Patricia Arquette
Release: 2025-01-12 09:49:46
Original
197 people have browsed it

How Do I Access Named Capturing Groups in .NET Regex Matches?

Accessing named capturing groups in .NET regular expressions

When parsing data using regular expressions in C#, capturing groups can be used to extract specific parts of a matching string. Named capturing groups allow easier and more meaningful access to these subgroups.

Question

Despite creating a regular expression with a named capture group, accessing a captured value using a CaptureCollection always returns the entire matching row.

Solution

To access a named capturing group, use the GroupCollection property of the Match object. This collection provides direct access to named subgroups within a match. The following code snippet demonstrates this:

<code class="language-C#">foreach (Match m in mc)
{
    // 访问 "link" 组
    MessageBox.Show(m.Groups["link"].Value);

    // 访问 "name" 组
    MessageBox.Show(m.Groups["name"].Value);
}</code>
Copy after login

By referencing the group name as an index in a GroupCollection, you can retrieve the captured value for each specified subgroup in the matching row.

The above is the detailed content of How Do I Access Named Capturing Groups in .NET Regex Matches?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template