Regular Expression class

Source: Internet
Author: User
The system. Text. regularexpressions namespace provides the Regular Expression Function, mainly including seven classes. The relationship between them is shown in the right figure. It is very complicated to write a lot of their functions and online materials. I divided it into four parts for definition:

Define a regular expression:
RegEx class
Return matching results:
Matchcollection class and Match Class
Return the results of the capture group:
Gropcollection class and group class
Return the result of the capture:
Capturecollection class and captrue class
At the same time, the last three items are the relationship between the set and the subset.

 

 

The methods and attributes of the RegEx class are very simple. They are mainly used for different constructors and are not easy to confuse.

The match class can use the match method of the RegEx class to return a match class object. In this case, the first matching item of the string is input, and nextmatch is not mentioned in many books, so many people think that the object using this class can only match the first item, in fact, all matching items can be traversed. Previously, I tried to make it easier without looking up any information. I just thought about the official materials. I thought it was the same as my thinking, but I thought about it again, you don't have to spend time thinking about it.

 

Usingsystem;
Usingsystem. Text. regularexpressions;
Namespacetmatch
{
Classprogram
{
Publicstaticvoidmain (string [] ARGs)
{
String S = "123abc456abcdabc123abc ";
Regexr = newregex ("ABC ");
Matchm = R. Match (s );
Inti = 1;
While (M. Success)
{
Console. writeline ("matching item {0}, matching value {1}-matching index {2}", I, m. Value, M. Index );
M = M. nextmatch ();
++ I;
}
Console. readkey (true );
}
}
}

 

The matchcollection class applies the regular expression pattern to the set of successful matches found in the input string in iterative mode. It is obtained through a specific call to the RegEx. Matches method. This technique can be used to access the Count attribute of a set (which is not available in the match class. This technology usually overhead the method of filling the set, which will have a great impact on the performance. You can simply traverse the objects returned by RegEx. Matches.
Usingsystem;
Usingsystem. Text. regularexpressions;
Namespacetmatchcollection
{
Classprogram
{
Publicstaticvoidmain (string [] ARGs)
{
String S = "123abc456abcdabc123abc ";
Matchcollectionmc = RegEx. Matches (S, "ABC ");
// Traverse matching items
For (INTI = 0; I <MC. Count; I ++)
{
Console. writeline ("No. {0} matches, matching value {1}-matching index {2}", I, MC [I]. value, MC [I]. index );
}
Console. readkey (true );
}

}
}
The static matches method is equivalent to constructing a RegEx object using the specified regular expression mode and calling the instance method matches. It is equivalent:
Match match = RegEx. Match (input, startat );
while (match.Success) 
 { 
// Handle match here...
 match = match.NextMatch();
 } 
Both the Group class and the gropcollection class are used to return a single capture group or collection class of the capture group. Therefore, you must first know what a capture group is. Otherwise, this class will be abused, the Groups Method of the Match Class obtains the set of groups matched by regular expressions. That is, the instance of the groupcollection class is returned. For example:

 

Using system;
Using system. Text. regularexpressions;
Namespace tmatchcollection
{
Class Program
{
Public static void main (string [] ARGs)
{
String input = "123abc456abcdabc123abc ";
String Pattern = "(A (B) c"; // three capture groups are defined here.
Match m = RegEx. Match (input, pattern );
Groupcollection GC = M. Groups; // gets the set of groups matched by regular expressions.
Int n = 1;
While (M. Success)
{
Console. writeline ("matching item {0}, matching value {1}-matching index {2}", n + 1, M. Value, M. Index );
M = M. nextmatch ();
++ N;
}
Console. writeline ("Number of captured groups: {0}", GC. Count );
// Let's check which capture groups are included.
For (INT I = 0; I <GC. Count; I ++)
{
Console. writeline ("{0}: Group {1}-starting index {2}", I + 1, GC [I]. Value, GC [I]. Index );
}
Console. readkey (true );
}
}
}

 

Here I will not go into details about the group class, because the group class instance has been implicitly used in the above example. An instance of the group class is returned by the match. Groups (groupname) attribute, or is in use (? <Groupname>...) returned by the match. Groups ("groupname") attribute when the group is constructed.

The capturecollection class indicates a collection of captured objects made by a capture group. Simply put, a capture group is executed to return one or more strings.
Note that it returns the capture results executed by a single capture group. For example:

Using system;
Using system. Text. regularexpressions;
Namespace Test
{
Class Program
{
Public static void main (string [] ARGs)
{
String input = "xyzabcabcabcxyzabcab ";
String Pattern = "(ABC) + ";
Const string posinfo = "starts with the {0} character ";
Match m = RegEx. Match (input, pattern );
Groupcollection GC = M. Groups; // gets the set of groups matched by regular expressions.
Console. writeline ("contains {0} capture groups \ n", GC. Count );
// Let's check which capture groups are included.
For (INT I = 0; I <GC. Count; I ++)
{
Capturecollection cc = GC [I]. captures; // return the capture results of a single capture group
Int cnum = cc. Count; // The total number of captured tasks in each group
Console. writeline ("the number of capture tasks in the {0} group is {1}", GC [I]. Value, CC. Count );
// Now we can view the specific information of the capture execution.
For (int ii = 0; II <cnum; II ++)
{
Console. writeline (CC [II]. Value + posinfo, CC [II]. Index );
}
Console. writeline ();
}
Console. readkey (true );
}
}
}

The capture class has been implicitly used in the example.

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.