Expression: bSS *
Star
shining
Example 2: Match words starting with c and ending with m
using System ; using System . Text . RegularExpressions ; namespace QTMCSharp { class Program { private static void showMatch ( string text , string expr ) { Console . WriteLine ( "Biểu thức: " + expr ); MatchCollection a = Regex . Matches ( text , expr ); foreach ( Match b in a ) { Console . WriteLine ( b ); } } static void Main ( string [] args ) { string str = "Quản trị mạng chấm com" ; Console . WriteLine ( "So khớp từ bắt đầu với 'c' và kết thúc với 'm':" ); showMatch ( str , @ "bcS*mb" ); Console . ReadKey (); } } }
Compiling and running the above C # program will produce the following output:
Match words starting with 'c' and ending with 'm':
Expressions: bcS * mb
dot
com
Example 3: Replace space (white space):
using System ; using System . Text . RegularExpressions ; namespace RegExApplication { class Program { static void Main ( string [] args ) { string input = " QTM chào bạn! " ; string pattern = "s+" ; string replacement = " " ; Regex rgx = new Regex ( pattern ); string result = rgx . Replace ( input , replacement ); Console . WriteLine ( "Chuỗi ban đầu: {0}" , input ); Console . WriteLine ( "Chuỗi đã thay thế khoảng trống: {0}" , result ); Console . ReadKey (); } } }
Compiling and running the above C # program will produce the following results:
Original string: QTM hello!
The string has replaced the gap: QTM greeted you!
According to Tutorialspoint
Previous article: Preprocessing directive in C #
Next lesson: Handling exceptions (Try / Catch / Finally) in C #