c# multi group in regex

  • 1375
  • 0
  • 2018-06-25

c# multi group in regex

In the example below, there is url group and name group in the Regex,

then you can directly get them by match.Groups["url"] and match.Groups["name"]

all the matches will contain these 2 groups.

Regex regex = new Regex("<a href=\"(?<url>.*)\">(?<name>.*)</a>", RegexOptions.IgnoreCase);

string result = "<A href=\"AAAAAA\">ss.BBBBB.ww</a>";

MatchCollection matches = regex.Matches(result);
if (matches.Count == 0)
{
	Console.WriteLine("parse failed.");
	return;
}

foreach (Match match in matches)
{
	if (!match.Success) { continue; }
	Console.WriteLine(match.Groups["name"]);
	Console.WriteLine(match.Groups["url"]);
}

 

Related Article

# Regular expression for finding 'href' value of a <a> link

# You can't parse [X]HTML with regex