1.regex
Regex re = new regex ("abc");
My understanding is: "ABC" this character to replace or judge ..., will be "ABC" as a concern.
For regular expressions, there are some code that are more commonly used.
#1忽略大小写的功能RegexOptions. IgnoreCase
3 stringSTR1 ="Hello";4 stringSTR2 ="HeLLo";5Regex re =NewRegex ("Hello", regexoptions.ignorecase);6 if(re. IsMatch (str1))7 {8Console.WriteLine ("It is match!");9 }Ten if(re. IsMatch (str2)) One { AConsole.WriteLine ("It is match,too!"); -}
The output is:
#2 replacement function Regex.Replace ()
1 stringSTR1 ="123abc321abc123";2Console.WriteLine ("before replace:{0}", str1);3Regex re =NewRegex ("ABC", regexoptions.ignorecase);4 stringNewstr = Re. Replace (STR1,"|");5Console.WriteLine ("After replace:{0}", NEWSTR);
The output is:
#3. Matching IP address
1 stringSTR1 ="04:09:54 111.13.100.91 www.baidu.com" 2+"05:22:23 112.25.24.135 www.sohu.com"3+"08:09:11 23.200.221.15 www.apple.com";4Regex re =NewRegex (@"(?<time> (\d|\:) +) \s"+5 @"(?<ip> (\d|\.) +) \s"+6 @"(? <site>\s+)");7MatchCollection matches =Re. Matches (STR1);8 foreach(Match matchinchmatches)9 {Ten if(Match. Length! =0) One { AConsole.WriteLine ("\nmatches: {0}", match. ToString ()); -Console.WriteLine ("Time : {0}", match. groups[" Time"]); -Console.WriteLine ("IP: {0}", match. groups["IP"]); theConsole.WriteLine ("site: {0}", match. groups["site"]); - } -}
Results:
2.async & await
This is a two keyword for asynchronous programming. write asynchronous methods just like write synchronous methods . The Async keyword can be used in the declarations section of a method, lambda expression, to indicate that this method might contain an await keyword, and only have async to use the await keyword inside it. When an async method, with an await keyword inside it, becomes an async method at compile time, and if there is no await keyword, it will only be executed as a synchronous method.
The following is an asynchronous comparator with Async & await from a web lookup to a normal sync. (You add the program run time)
First Sync:
1 Static voidMain (string[] args)2 { 3DateTime D1 =DateTime.Now;4 //Synchronization Mode5Console.WriteLine ("sync mode Test starts! ");6Syncmethod (0);7Console.WriteLine ("sync way to end! ");8 9DateTime D2 =DateTime.Now;TenConsole.WriteLine ("time to spend: {0}", d2-D1); One A } - //Synchronous Operation - Private Static voidSyncmethod (intinput) the { -Console.WriteLine ("Enter the sync operation! "); - varresult =syancwork (input); -Console.WriteLine ("end result {0}", result); +Console.WriteLine ("quit the sync operation! "); - } + //simulate time-consuming operations (synchronous method) A Private Static intSyancwork (intval) at { - for(inti =0; I <5; ++i) - { -Console.WriteLine ("time-consuming operation {0}", i); -Thread.Sleep ( -); -val++; in } - returnVal; to}
Results:
Then it is asynchronous:
1 Static voidMain (string[] args)2 {3 //Async Mode4DateTime D1 =DateTime.Now;5Console.WriteLine ("\ n Async mode Test begins! ");6Asyncmethod (0);7Console.WriteLine ("asynchronous way to end! ");8 Console.readkey ();9DateTime D2 =DateTime.Now;TenConsole.WriteLine ("program run time: {0}", d2-D1); One } A - //Asynchronous Operation - Private Static Async voidAsyncmethod (intinput) the { -Console.WriteLine ("Enter the asynchronous operation! "); - varresult =awaitasyncwork (input); -Console.WriteLine ("end result {0}", result); +Console.WriteLine ("quit the asynchronous operation! "); - } + A //simulate time-consuming operations (async methods) at Private Static Asynctask<int> Asyncwork (intval) - { - for(inti =0; I <5; ++i) - { -Console.WriteLine ("time-consuming operation {0}", i); - awaitTask.delay ( -); inval++; - } to returnVal; +}
The result is:
The result shows that the time-consuming operation has become asynchronous, that is, it does not wait for the following asyncwork to complete its time-consuming operation, return to the main function, and then take the time-consuming action. After comparison, the structure of the two programs, just asynchronous more than two keywords async & await, you can write synchronous method to write asynchronous program.
Over
Regex & Async & await