The class for regular expressions in C # is contained in the System.Text.RegularExpressions namespace, which can be added by code:
1 using System.Text.RegularExpressions;
View Code
In string matching, the protagonists are "matched strings" and "matching patterns".
1 //define "matched string"2 stringMessage ="mother father Sister brother";3 //define "match pattern"4 stringPattern =@"\b (\w+) ther\b";5 //match by static method, or by creating a regular object6MatchCollection matches =regex.matches (message, pattern);7 //the resulting traversal8 foreach(Match matchinchmatches) {9Console.WriteLine ("Groups[0]. Value = {0}---groups[1]. Value = {1}", match. groups[0]. Value, matchgroups[1]. Value); Ten } One A //Output - //Groups[0]. Value = Mother---groups[1]. Value = Mo - //Groups[0]. Value = Father---groups[1]. Value = FA the //Groups[0]. Value = Brother---groups[1]. Value = Bro - //End Output
This is one of the examples where there are three ways to match: IsMatch (), Match (), Matches ().
If you just want to know if the "matched string" contains a certain "match pattern", you can use IsMatch ();
If you want a match to the result, you can use match ();
If you want to get multiple matching results, you can use matches ();
A single match to the result can use groups[] to get a grouping that matches the matching pattern to take advantage of the information that is being matched.
Specific usage rules: Http://www.dotnetperls.com/regex-match
Simple use of regular expressions in C #