How do I access named capturing groups in a .NET Regex?

I’m having a hard time finding a good resource that explains how to use Named Capturing Groups in C#. This is the code that I have so far:

string page = Encoding.ASCII.GetString(bytePage);
Regex qariRegex = new Regex("<td><a href=\"(?<link>.*?)\">(?<name>.*?)</a></td>");
MatchCollection mc = qariRegex.Matches(page);
CaptureCollection cc = mc[0].Captures;
MessageBox.Show(cc[0].ToString());

However this always just shows the full line:

<td><a href="https://stackoverflow.com/path/to/file">Name of File</a></td> 

I have experimented with several other “methods” that I’ve found on various websites but I keep getting the same result.

How can I access the named capturing groups that are specified in my regex?

5 Answers
5

Leave a Comment