C # Regular expression class match and group class understanding

Source: Internet
Author: User

A regular expression can be viewed as a small programming language with a specific function that locates substrings in a paragraph of text. Regular expressions allow you to quickly parse large amounts of text to find specific character patterns, extract, edit, replace, or delete text substrings, or add extracted strings to the collection. The basic syntax for regular expressions can be seen in simple, regular expressions (i), and in the regular expression of the English language (II.).

The C # namespace System.Text.RegularExpressions provides classes that support regular expression operations. These classes mainly include regex,matchcollection,match,groupcollection,group,caputercollection and Caputer, and the following diagram shows the relationships between these classes.

One important application of regular expressions is to extract strings in the text, which is achieved primarily by the match class and group class, so it is important to understand the concepts of matching and groups. To find the URL feature in a piece of text, this example is simpler, as long as you call the Regex.Matches () method to find a collection of URLs. The sample code is as follows:

That extracts the URL in the text.

public static void Main (string[] args)
{
String text = "I ' ve found this amazing URL to http://www.sohu.com, and then find Ftp://ftp.sohu.com is better.";
String pattern = @ "\b (\s+):///(\s+) \b"; Patterns that match URLs
MatchCollection mc = regex.matches (text, pattern); Matching set of pattern match
Console.WriteLine ("The URL address contained in the text is:");
foreach (Match match in MC)
{
Console.WriteLine (match. ToString ());

Console.read ();
}
/*
* After running the output is as follows:
* The URL address contained in the text is:
* http://www.sohu.com
* ftp://ftp.sohu.com
*/}

Now, the requirements change, not only to find the URL, but also to find the protocol and domain name address of each URL, then use the regular expression of the grouping function. The grouping is to match pattern patterns in parentheses, grouped into different groups, such as changing the pattern in the above example to: string patterns = @ "\b (? <protocol>\s+)://(? <address>\s+) \b "; This is divided into two groups (actually three groups, because the match itself can be seen as a large group), "?<protocol>" and "?<address>" define each group's alias protocol and address, which is not necessary, It's just easy for us to get the group we need. The sample code is as follows:

Example of extracting URL protocols and addresses

public static void Main (string[] args)
{
String text = "I ' ve found this amazing URL to http://www.sohu.com, and then find Ftp://ftp.sohu.com is better.";
String pattern = @ "\b (? <protocol>\s+)://(? <address>\s+) \b"; Matches the pattern of URLs and groups
MatchCollection mc = regex.matches (text, pattern); Matching set of pattern match
Console.WriteLine ("The URL address contained in the text is:");
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 ();
}
/**
* After running the output is as follows:
* The URL address contained in the text is:
* url:http://www.sohu.com;protocol:http;address:www.sohu.com
* url:ftp://ftp.sohu.com;protocol:ftp;address:ftp.sohu.com
*/

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.