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!