C # understanding of regular expressions like match and group

Source: Internet
Author: User

Regular Expressions can be seen as small ones with specific functionsProgramming LanguageTo locate the substring in a piece of text. Regular Expressions can be used to quickly analyze a large amount of text to find a specific character pattern; extract, edit, replace or delete the text character string; or add the extracted string to the set. For the basic syntax of a regular expression, see go deep into the regular expression (1) and go deep into the regular expression (2 ).

C # namespace system. Text. regularexpressions provides classes that support regular expression operations. These classes mainly include RegEx, matchcollection, match, groupcollection, group, caputercollection, and caputer, which indicate the relationships between these classes.

 

A very important application of regular expressions is to extract strings in the text. The implementation of this function mainly relies on the match class and the group class. Therefore, it is important to understand the concept of matching and group. To implement the URL search function in a piece of text, this example is relatively simple. You only need to call the RegEx. Matches () method to find the URL set. ExampleCodeAs follows:

Extract the URL
Public   Static   Void Main ( String [] ARGs)
{
String Text =   " 'I ve found this amazing URL at http://www.sohu.com, and then find ftp://ftp.sohu.com is better. " ;
String Pattern =   @" \ B (\ s +): // (\ s +) \ B " ; // URL matching mode
Matchcollection MC = RegEx. Matches (text, pattern ); // Matching set that meets Pattern
Console. writeline ( " The URL addresses in the text include: " );
Foreach (Match In MC)
{
Console. writeline (match. tostring ());
}

console. read ();
}< br> /*
* output after running:
* the URL addresses in the text include:
* http://www.sohu.com
* ftp://ftp.sohu.com
*/

Now, the requirement has changed. Not only do you need to find the URL, but also the Protocol and domain name address of each URL. In this case, you need to use the grouping function of the regular expression. Group is the pattern to be matched. The pattern is enclosed in parentheses and divided into different groups. For example, you can change the pattern in the preceding example to: String Pattern = @ "\ B (? <Protocol> \ s + )://(? <Address> \ s +) \ B "; in this way, brackets are divided into two groups (in fact, three groups, because the matching itself can be considered as a large group ),"? <Protocol> "and "? <Address> "defines the aliases protocol and address of each group. This is not necessary, but it is convenient for us to obtain the desired group. The sample code is as follows:

Example of extracting URL protocol and address
Public   Static   Void Main ( String [] ARGs)
{
String Text =   " 'I ve found this amazing URL at http://www.sohu.com, and then find ftp://ftp.sohu.com is better. " ;
String Pattern =   @" \ B (? <Protocol> \ s + )://(? <Address> \ s +) \ B " ; // Match URL pattern and group
Matchcollection MC = RegEx. Matches (text, pattern ); // Matching set that meets Pattern

Console. writeline ( " The URL addresses in the text include: " );
Foreach (Match In MC)
{
Groupcollection GC = Match. Groups;
String Outputtext =   " URL: "   + Match. Value +   " ; Protocol: "   + GC [ " Protocol " ]. Value +   " ; Address: "   + GC [ " Address " ]. value;
console. writeline (outputtext);
}< P>

console. read ();
}< br> /* *
* output after running:
* the URL 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
*/

reference: use regular expressions to obtain arrays, C # use regular expressions to search for strings and explore Regular Expressions

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.