usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Reflection;usingSystem.Text.RegularExpressions;namespacecodetest{classProgram {Static voidMain (string[] args) { string[] Strarray = {"1111-222-333","1111-2222-3333" }; stringPattern =@"^\d{4}-\d{3}-\d{3}$"; foreach(varIteminchStrarray) { if(Regex.IsMatch (item, pattern)) {Console.WriteLine ("{0} is vaild", item); } Else{Console.WriteLine ("{0} is not vaild", item); }} regexismacth (); Regexreplace (); Regexsplit (); Regexmacths (); Regexgroup (); Console.ReadLine (); } Static voidregexismacth () {stringinput ="This is my name my name"; stringPattern =@"\s\w{4}\s"; Match Match=Regex.match (Input,pattern); while(match. Success) {Console.WriteLine (match. groups[0]. Value); Match=match. NextMatch (); } } Static voidRegexreplace () {stringinput ="This is my name my name"; stringPattern =@"\s\w{4}\s"; stringReplacement ="Match"; Console.WriteLine (input); Console.WriteLine (Regex.Replace (input, pattern, replacement)); } Static voidRegexsplit () {stringinput ="1.A 2.B 3.C 4.D"; stringPattern =@"\s"; foreach(varIteminchRegex.Split (Input,pattern)) { if(!string. IsNullOrEmpty (item)) {Console.WriteLine (item); } } } Static voidregexmacths () {regex regex=NewRegex (@"\d+"); MatchCollection matchs; Matchs= Regex. Matches ("123abc333adsa123123asdasd123123"); foreach(Match Iteminchmatchs) {Console.WriteLine ("value is {0},index is {1}", item. Value,item. Index); Console.WriteLine ("Result is {0}", item. Result ("[$&]")); } } Static voidRegexgroup () {stringinput ="Born:july, 1989"; stringPattern =@"\b (\w+) \s (\d{1,2}), \s (\d{4}) \b"; Match Match=Regex.match (input, pattern); for(inti =0; I < match. Groups.count; i++) {Console.WriteLine ("Group value is {0},index is {1}", match. Groups[i]. Value,match. Groups[i]. Index); } } }}
Recommended use of Microsoft's regular Expression tester tool test
Regular Expressions for C #