C # Regular expression class match class and group class

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.