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>
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!