Match class
Example: Finding URLs that are contained in a string
String text = "firsturl:http://www.sohu.com, secondurl:http://www.baidu.com";
String pattern = @ "\b (\s+)://(\s+) \b"; Patterns that match URLs
MatchCollection mc = regex.matches (text, pattern); Matching sets that satisfy pattern
Console.WriteLine ("URL addresses included in the text are:");
foreach (Match match in MC)
{
Console.WriteLine (match. Value);
}
Console.ReadLine ();
Results:
Group class
Example: Find the URL contained in the string and find out the protocol and domain name address for each URL
String text = "firsturl:http://www.sohu.com, secondurl:http://www.baidu.com";
String pattern = @ "\b (? <protocol>\s+)://(? <address>\s+) \b"; Matches the pattern of the URL and groups
MatchCollection mc = regex.matches (text, pattern); Matching sets that satisfy pattern
Console.WriteLine ("URL addresses included in the text are:");
foreach (Match match in MC)
{
GroupCollection gc = match. Groups;
String outputtext = "URL:" + match. Value + "; Protocol:" + gc["Protocol"]. Value + "; Address:" + gc["address"]. Value;
Console.WriteLine (Outputtext);
}
Console.read ();
Description: "?<protocol>" and "?<address>" define aliases for each group protocol and address
C # Regular expression class match class and group class