Trang chủ
Bài viết mới
Diễn đàn
Bài mới trên hồ sơ
Hoạt động mới nhất
VIDEO
Mùa Tết
Văn Học Trẻ
Văn Học News
Media
New media
New comments
Search media
Đại Học
Đại cương
Chuyên ngành
Triết học
Kinh tế
KHXH & NV
Công nghệ thông tin
Khoa học kĩ thuật
Luận văn, tiểu luận
Phổ Thông
Lớp 12
Ngữ văn 12
Lớp 11
Ngữ văn 11
Lớp 10
Ngữ văn 10
LỚP 9
Ngữ văn 9
Lớp 8
Ngữ văn 8
Lớp 7
Ngữ văn 7
Lớp 6
Ngữ văn 6
Tiểu học
Thành viên
Thành viên trực tuyến
Bài mới trên hồ sơ
Tìm trong hồ sơ cá nhân
Credits
Transactions
Xu: 0
Đăng nhập
Đăng ký
Có gì mới?
Tìm kiếm
Tìm kiếm
Chỉ tìm trong tiêu đề
Bởi:
Hoạt động mới nhất
Đăng ký
Menu
Đăng nhập
Đăng ký
Install the app
Cài đặt
Chào mừng Bạn tham gia Diễn Đàn VNKienThuc.com -
Định hướng Forum
Kiến Thức
- HÃY TẠO CHỦ ĐỀ KIẾN THỨC HỮU ÍCH VÀ CÙNG NHAU THẢO LUẬN Kết nối:
VNK X
-
VNK groups
| Nhà Tài Trợ:
BhnongFood X
-
Bhnong groups
-
Đặt mua Bánh Bhnong
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Regular Expression là gì và cách sử dụng nó trong C#
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Trả lời chủ đề
Nội dung
<blockquote data-quote="uocmo_kchodoi" data-source="post: 177558" data-attributes="member: 165510"><p style="text-align: center"><strong><span style="color: #ff0000">REGULAR EXPRESSION TRONG C#</span></strong></p> <p style="text-align: center"><strong><span style="color: #ff0000"></span></strong></p><p><span style="color: #000000"><strong>Khái niệm: </strong></span><span style="color: rgb(0, 0, 0)">Regular Expression hay tiếng Việt được gọi là Biểu thức chính quy, là một cấu trúc rất mạnh để mô tả một chuỗi theo cách thống nhất chung.</span></p><p><span style="color: #000000"></span></p><p><span style="color: #000000">Regular Expression bao gồm tập hợp các ký tự, toán tử hay ký hiệu toán học nhằm biểu thị một chuỗi theo cấu trúc chung mà mọi người học theo. Có thể xem Regular Expression như một loại tiếng lóng dùng chung trong lập trình.</span></p><p><span style="color: #000000"></span></p><p><span style="color: #000000">Bạn có thể trích lọc một hay nhiều chuỗi có cấu trúc chung từ một đoạn văn bản hay một chuỗi ra.</span></p><p><span style="color: #000000"></span></p><p><span style="color: #000000">Bạn có thể tìm kiếm, thay đổi nội dung của chuỗi một cách dễ dàng. Thay vì phải ngồi cắt chuỗi mỏi mệt như trước đây.</span></p><p><span style="color: #000000"></span></p><p><span style="color: #000000">Từ đây, với Regular Expression. Bạn hoàn toàn có thể trích lọc dữ liệu từ các đoạn html theo ý.</span></p><p><span style="color: #000000"></span></p><p><span style="color: #000000"><span style="font-size: 18px"><strong>1. Construct cho định nghĩa Regular Expression trong C#</strong></span></span></p><p><span style="color: #000000"><span style="font-size: 18px"></span></span></p><p><span style="color: #000000">Có nhiều loại ký tự, toán tử và construct đa dạng mà giúp bạn định nghĩa Regular Expression trong C#. Bạn theo các link sau để có chi tiết về các construct này.</span></p><ul> <li data-xf-list-type="ul"><span style="color: #000000">Character escape <a href="https://vnkienthuc.com/su-dung-escape-character-trong-c-nhu-the-nao.t77322/" target="_blank">https://vnkienthuc.com/su-dung-escape-character-trong-c-nhu-the-nao.t77322/</a></span></li> <li data-xf-list-type="ul"><span style="color: #000000">Lớp Character <a href="https://vnkienthuc.com/su-dung-lop-character-trong-c-nhu-the-nao.t77323/" target="_blank">https://vnkienthuc.com/su-dung-lop-character-trong-c-nhu-the-nao.t77323/</a> </span></li> <li data-xf-list-type="ul"><span style="color: #000000">Anchor </span></li> <li data-xf-list-type="ul"><span style="color: #000000">Grouping construct <a href="https://vnkienthuc.com/grouping-construct-trong-c-la-gi.t77325/" target="_blank">https://vnkienthuc.com/grouping-construct-trong-c-la-gi.t77325/</a> </span></li> <li data-xf-list-type="ul"><span style="color: #000000">Quantifier <a href="https://vnkienthuc.com/quantifier-trong-c-la-gi.t77326/" target="_blank">https://vnkienthuc.com/quantifier-trong-c-la-gi.t77326/</a></span></li> <li data-xf-list-type="ul"><span style="color: #000000">Backreference construct <a href="https://vnkienthuc.com/backreference-construct-trong-c-la-gi.t77327/" target="_blank">https://vnkienthuc.com/backreference-construct-trong-c-la-gi.t77327/</a></span></li> <li data-xf-list-type="ul"><span style="color: #000000">Alternation construct <a href="https://vnkienthuc.com/alternation-construct-trong-c-la-gi.t77328/" target="_blank">https://vnkienthuc.com/alternation-construct-trong-c-la-gi.t77328/</a></span></li> <li data-xf-list-type="ul"><span style="color: #000000">Subsititution <a href="https://vnkienthuc.com/substitution-trong-c-la-gi.t77329/" target="_blank">https://vnkienthuc.com/substitution-trong-c-la-gi.t77329/</a></span></li> <li data-xf-list-type="ul"><span style="color: #000000">Miscellaneous constructs <a href="https://vnkienthuc.com/construct-hon-hop-trong-c.t77330/" target="_blank">https://vnkienthuc.com/construct-hon-hop-trong-c.t77330/</a></span></li> </ul><p><span style="color: #000000"><span style="font-size: 18px"><strong>2. Lớp Regex trong C#</strong></span></span></p><p><span style="color: #000000"><span style="font-size: 18px"></span></span></p><p><span style="color: #000000">Lớp Regex trong C# được sử dụng để biểu diễn một Regular Expression. Nó có các phương thức được sử dụng phổ biến sau:</span></p><p></p><p><span style="color: #0000ff">1. public bool IsMatch(string input)</span></p><p><span style="color: #000000">Chỉ rằng có hay không Regular Expression đã cho trong Regex constructor này tìm thấy một match trong chuỗi đầu vào đã xác định</span></p><p><span style="color: #000000"></span></p><p><span style="color: #0000ff">2. public bool IsMatch(string input, int startat)</span></p><p><span style="color: rgb(0, 0, 0)">Chỉ rằng có hay không Regular Expression đã cho trong Regex constructor này tìm thấy một match trong chuỗi đầu vào đã xác định, bắt đầu tại vị trí startat đã cho trong chuỗi</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p><span style="color: #0000ff">3. public static bool IsMatch(string input, string pattern)</span></p><p><span style="color: rgb(0, 0, 0)">Chỉ rằng có hay không Regular Expression đã cho tìm thấy một match trong chuỗi đầu vào đã xác định</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p><span style="color: #0000ff">4. public MatchCollection Matches(string input)</span></p><p><span style="color: rgb(0, 0, 0)">Tìm kiếm chuỗi đầu vào đã xác định về tất cả sự xuất hiện của một Regular Expression</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p><span style="color: #0000ff">5. public string Replace(string input, string replacement)</span></p><p><span style="color: rgb(0, 0, 0)">Trong một chuỗi đầu vào đã xác định, thay thế tất cả chuỗi mà so khớp với một Regular Expression pattern với một chuỗi thay thế đã cho</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p><span style="color: #0000ff">6. public string[] Split(string input)</span></p><p><span style="color: rgb(0, 0, 0)">Chia một chuỗi đầu vào thành một mảng các chuỗi phụ tại vị trí được định nghĩa bởi một Regular Expression pattern đã xác định trong Regex constructor</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p><span style="color: rgb(0, 0, 0)">Để có danh sách đầy đủ các phương thức và thuộc tính, bạn vui lòng đọc Microsoft Documentation về C#.</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p><span style="color: rgb(0, 0, 0)"><span style="font-size: 18px"><strong>Ví dụ 1</strong></span></span></p><p><span style="color: rgb(0, 0, 0)"><span style="font-size: 18px"></span></span></p><p><span style="color: rgb(0, 0, 0)">Ví dụ sau so khớp với các từ mà bắt đầu với 'S':</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p><em><span style="color: #5900b3">using System;</span></em></p><p><em><span style="color: #5900b3">using System.Text.RegularExpressions;</span></em></p><p><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></p><p><em><span style="color: #5900b3">{</span></em></p><p><em><span style="color: #5900b3"> class TestCsharp</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> private static void showMatch(string text, string expr)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Expression: " + expr);</span></em></p><p><em><span style="color: #5900b3"> MatchCollection mc = Regex.Matches(text, expr);</span></em></p><p><em><span style="color: #5900b3"> foreach (Match m in mc)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine(m);</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Vi du minh hoa Regular Expression trong C#");</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("-----------------------------------------");</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> string str = "Sao hom nay lai thay sao suyen son sang";</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("So khop voi cac tu bat dau bang chu cai 'S': ");</span></em></p><p><em><span style="color: #5900b3"> showMatch(str, @"\bS\S*");</span></em></p><p><em><span style="color: #5900b3"> Console.ReadKey();</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3">}</span></em></p><p><span style="color: rgb(0, 0, 0)">Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn.</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p><span style="color: rgb(0, 0, 0)">Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p style="text-align: center"><img src="https://vietjack.com/csharp/images/regurlar-expression.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p><p><span style="color: rgb(0, 0, 0)"><span style="font-size: 18px"><strong>Ví dụ 2</strong></span></span></p><p><span style="color: rgb(0, 0, 0)"><span style="font-size: 18px"></span></span></p><p><span style="color: rgb(0, 0, 0)">Ví dụ sau so khớp với các từ mà bắt đầu với 's' và kết thúc với 'n':</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p><em><span style="color: #5900b3">using System;</span></em></p><p><em><span style="color: #5900b3">using System.Text.RegularExpressions;</span></em></p><p><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></p><p><em><span style="color: #5900b3">{</span></em></p><p><em><span style="color: #5900b3"> class TestCsharp</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> private static void showMatch(string text, string expr)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Expression: " + expr);</span></em></p><p><em><span style="color: #5900b3"> MatchCollection mc = Regex.Matches(text, expr);</span></em></p><p><em><span style="color: #5900b3"> foreach (Match m in mc)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine(m);</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Vi du minh hoa Regular Expression trong C#");</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("-----------------------------------------");</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> string str = "Sao hom nay lai thay sao suyen son sang";</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("So khop voi tu bat dau bang 's' va ket thuc bang 'n':");</span></em></p><p><em><span style="color: #5900b3"> showMatch(str, @"\bs\S*n\b");</span></em></p><p><em><span style="color: #5900b3"> Console.ReadKey();</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3">}</span></em></p><p><span style="color: rgb(0, 0, 0)">Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p style="text-align: center"><img src="https://vietjack.com/csharp/images/regurlar-expression-1.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p><p><span style="color: rgb(0, 0, 0)"><span style="font-size: 18px"><strong>Ví dụ 3</strong></span></span></p><p><span style="color: rgb(0, 0, 0)"><span style="font-size: 18px"></span></span></p><p><span style="color: rgb(0, 0, 0)">Ví dụ sau thay thế các white space:</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p><em><span style="color: #5900b3">using System;</span></em></p><p><em><span style="color: #5900b3">using System.Text.RegularExpressions;</span></em></p><p><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></p><p><em><span style="color: #5900b3">{</span></em></p><p><em><span style="color: #5900b3"> class TestCsharp</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Vi du minh hoa Regular Expression trong C#");</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("-----------------------------------------");</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> string str1 = "Hello World ";</span></em></p><p><em><span style="color: #5900b3"> string pattern = "\\s+";</span></em></p><p><em><span style="color: #5900b3"> string str2 = " ";</span></em></p><p><em><span style="color: #5900b3"> Regex rgx = new Regex(pattern);</span></em></p><p><em><span style="color: #5900b3"> string result = rgx.Replace(str1, str2);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Chuoi ban dau: {0}", str1);</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Chuoi sau khi da thay the: {0}", result);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> Console.ReadKey();</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3">}</span></em></p><p><span style="color: rgb(0, 0, 0)">Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</span></p><p><span style="color: rgb(0, 0, 0)"></span></p><p style="text-align: center"><img src="https://vietjack.com/csharp/images/regurlar-expression-1.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p> <p style="text-align: center"></p> <p style="text-align: right">Nguồn: vietjack.com</p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177558, member: 165510"] [CENTER][B][COLOR=#ff0000]REGULAR EXPRESSION TRONG C# [/COLOR][/B][/CENTER] [COLOR=#000000][B]Khái niệm: [/B][/COLOR][COLOR=rgb(0, 0, 0)]Regular Expression hay tiếng Việt được gọi là Biểu thức chính quy, là một cấu trúc rất mạnh để mô tả một chuỗi theo cách thống nhất chung.[/COLOR] [COLOR=#000000] Regular Expression bao gồm tập hợp các ký tự, toán tử hay ký hiệu toán học nhằm biểu thị một chuỗi theo cấu trúc chung mà mọi người học theo. Có thể xem Regular Expression như một loại tiếng lóng dùng chung trong lập trình. Bạn có thể trích lọc một hay nhiều chuỗi có cấu trúc chung từ một đoạn văn bản hay một chuỗi ra. Bạn có thể tìm kiếm, thay đổi nội dung của chuỗi một cách dễ dàng. Thay vì phải ngồi cắt chuỗi mỏi mệt như trước đây. Từ đây, với Regular Expression. Bạn hoàn toàn có thể trích lọc dữ liệu từ các đoạn html theo ý. [SIZE=5][B]1. Construct cho định nghĩa Regular Expression trong C#[/B] [/SIZE] Có nhiều loại ký tự, toán tử và construct đa dạng mà giúp bạn định nghĩa Regular Expression trong C#. Bạn theo các link sau để có chi tiết về các construct này.[/COLOR] [LIST] [*][COLOR=#000000]Character escape [URL]https://vnkienthuc.com/su-dung-escape-character-trong-c-nhu-the-nao.t77322/[/URL][/COLOR] [*][COLOR=#000000]Lớp Character [URL]https://vnkienthuc.com/su-dung-lop-character-trong-c-nhu-the-nao.t77323/[/URL] [/COLOR] [*][COLOR=#000000]Anchor [/COLOR] [*][COLOR=#000000]Grouping construct [URL]https://vnkienthuc.com/grouping-construct-trong-c-la-gi.t77325/[/URL] [/COLOR] [*][COLOR=#000000]Quantifier [URL]https://vnkienthuc.com/quantifier-trong-c-la-gi.t77326/[/URL][/COLOR] [*][COLOR=#000000]Backreference construct [URL]https://vnkienthuc.com/backreference-construct-trong-c-la-gi.t77327/[/URL][/COLOR] [*][COLOR=#000000]Alternation construct [URL]https://vnkienthuc.com/alternation-construct-trong-c-la-gi.t77328/[/URL][/COLOR] [*][COLOR=#000000]Subsititution [URL]https://vnkienthuc.com/substitution-trong-c-la-gi.t77329/[/URL][/COLOR] [*][COLOR=#000000]Miscellaneous constructs [URL]https://vnkienthuc.com/construct-hon-hop-trong-c.t77330/[/URL][/COLOR] [/LIST] [COLOR=#000000][SIZE=5][B]2. Lớp Regex trong C#[/B] [/SIZE] Lớp Regex trong C# được sử dụng để biểu diễn một Regular Expression. Nó có các phương thức được sử dụng phổ biến sau:[/COLOR] [COLOR=#0000ff]1. public bool IsMatch(string input)[/COLOR] [COLOR=#000000]Chỉ rằng có hay không Regular Expression đã cho trong Regex constructor này tìm thấy một match trong chuỗi đầu vào đã xác định [/COLOR] [COLOR=#0000ff]2. public bool IsMatch(string input, int startat)[/COLOR] [COLOR=rgb(0, 0, 0)]Chỉ rằng có hay không Regular Expression đã cho trong Regex constructor này tìm thấy một match trong chuỗi đầu vào đã xác định, bắt đầu tại vị trí startat đã cho trong chuỗi [/COLOR] [COLOR=#0000ff]3. public static bool IsMatch(string input, string pattern)[/COLOR] [COLOR=rgb(0, 0, 0)]Chỉ rằng có hay không Regular Expression đã cho tìm thấy một match trong chuỗi đầu vào đã xác định [/COLOR] [COLOR=#0000ff]4. public MatchCollection Matches(string input)[/COLOR] [COLOR=rgb(0, 0, 0)]Tìm kiếm chuỗi đầu vào đã xác định về tất cả sự xuất hiện của một Regular Expression [/COLOR] [COLOR=#0000ff]5. public string Replace(string input, string replacement)[/COLOR] [COLOR=rgb(0, 0, 0)]Trong một chuỗi đầu vào đã xác định, thay thế tất cả chuỗi mà so khớp với một Regular Expression pattern với một chuỗi thay thế đã cho [/COLOR] [COLOR=#0000ff]6. public string[] Split(string input)[/COLOR] [COLOR=rgb(0, 0, 0)]Chia một chuỗi đầu vào thành một mảng các chuỗi phụ tại vị trí được định nghĩa bởi một Regular Expression pattern đã xác định trong Regex constructor Để có danh sách đầy đủ các phương thức và thuộc tính, bạn vui lòng đọc Microsoft Documentation về C#. [SIZE=5][B]Ví dụ 1[/B] [/SIZE] Ví dụ sau so khớp với các từ mà bắt đầu với 'S': [/COLOR] [I][COLOR=#5900b3]using System; using System.Text.RegularExpressions; namespace VietJackCsharp { class TestCsharp { private static void showMatch(string text, string expr) { Console.WriteLine("Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); foreach (Match m in mc) { Console.WriteLine(m); } } static void Main(string[] args) { Console.WriteLine("Vi du minh hoa Regular Expression trong C#"); Console.WriteLine("-----------------------------------------"); string str = "Sao hom nay lai thay sao suyen son sang"; Console.WriteLine("So khop voi cac tu bat dau bang chu cai 'S': "); showMatch(str, @"\bS\S*"); Console.ReadKey(); } } }[/COLOR][/I] [COLOR=rgb(0, 0, 0)]Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn. Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [/COLOR] [CENTER][IMG]https://vietjack.com/csharp/images/regurlar-expression.PNG[/IMG][/CENTER] [COLOR=rgb(0, 0, 0)][SIZE=5][B]Ví dụ 2[/B] [/SIZE] Ví dụ sau so khớp với các từ mà bắt đầu với 's' và kết thúc với 'n': [/COLOR] [I][COLOR=#5900b3]using System; using System.Text.RegularExpressions; namespace VietJackCsharp { class TestCsharp { private static void showMatch(string text, string expr) { Console.WriteLine("Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); foreach (Match m in mc) { Console.WriteLine(m); } } static void Main(string[] args) { Console.WriteLine("Vi du minh hoa Regular Expression trong C#"); Console.WriteLine("-----------------------------------------"); string str = "Sao hom nay lai thay sao suyen son sang"; Console.WriteLine("So khop voi tu bat dau bang 's' va ket thuc bang 'n':"); showMatch(str, @"\bs\S*n\b"); Console.ReadKey(); } } }[/COLOR][/I] [COLOR=rgb(0, 0, 0)]Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [/COLOR] [CENTER][IMG]https://vietjack.com/csharp/images/regurlar-expression-1.PNG[/IMG][/CENTER] [COLOR=rgb(0, 0, 0)][SIZE=5][B]Ví dụ 3[/B] [/SIZE] Ví dụ sau thay thế các white space: [/COLOR] [I][COLOR=#5900b3]using System; using System.Text.RegularExpressions; namespace VietJackCsharp { class TestCsharp { static void Main(string[] args) { Console.WriteLine("Vi du minh hoa Regular Expression trong C#"); Console.WriteLine("-----------------------------------------"); string str1 = "Hello World "; string pattern = "\\s+"; string str2 = " "; Regex rgx = new Regex(pattern); string result = rgx.Replace(str1, str2); Console.WriteLine("Chuoi ban dau: {0}", str1); Console.WriteLine("Chuoi sau khi da thay the: {0}", result); Console.ReadKey(); } } }[/COLOR][/I] [COLOR=rgb(0, 0, 0)]Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [/COLOR] [CENTER][IMG]https://vietjack.com/csharp/images/regurlar-expression-1.PNG[/IMG] [COLOR=#000000][/COLOR][/CENTER] [RIGHT]Nguồn: vietjack.com[/RIGHT] [/QUOTE]
Tên
Mã xác nhận
Gửi trả lời
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Regular Expression là gì và cách sử dụng nó trong C#
Top