1 / 27

CHAPTER 2:

CHAPTER 2:. REGULAR EXPRESSION (BIỂU THỨC CHÍNH QUY) C# 2010 for programmer chapter 16 in page 567. Nội dung. Khái niệm và vai trò của biểu thức chính qui. Các class hỗ trợ trong C# Lớp ký tự dùng trong biểu thức chính qui và các ví dụ: Ký tự định dạng cho từng loại văn bản. Quantifiers

zelig
Download Presentation

CHAPTER 2:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 2: REGULAR EXPRESSION (BIỂU THỨC CHÍNH QUY) C# 2010 for programmer chapter 16 in page 567

  2. Nội dung • Khái niệm và vai trò của biểu thức chính qui. • Các class hỗ trợ trong C# • Lớp ký tự dùng trong biểu thức chính qui và các ví dụ: • Ký tự định dạng cho từng loại văn bản. • Quantifiers • Group, phép Or

  3. 1 - Regular Expression là gì? • Là công cụ ngôn ngữ mạnh, là một phần của các ngôn ngữ lập trình hiện đại. • Regular expression là chuỗi mẫu chứa các ký tự định dạng dùng để xác định xem văn bản có thỏa định dạng đưa ra không nhằm đảm bảo các chuỗi có định dạng chung nào đó. • Ví dụ: kiểm tra ZIP code, địa chỉ email, họ tên hợp lệ...

  4. 2. Các class hỗ trợ: • .NET framework cung cấp các classes trong System.Text.RegularExpression: • Regex class: • Có phương thức Match (static hoặc instance) trả về kết quả là các so trùng kiểu class Match • Matches trả về MatchCollection object • Match class • Kiểu chứa một kết quả match tìm thấy. • ToString() trả về chuỗi con match với regular expression • MatchCollection class • Kiểu chứa tập nhiều match tìm thấy.

  5. Sử dụng Regex để tìm kiếm: Regex regexObject= new Regex(Chuỗi_Mẫu); Match matchObject= regexObject.Match(Chuỗi_Văn_Bản); MatchCollection matchsObj=regexObject.Matches(Chuỗi_Văn_Bản); Match: Khi tìm thấy chuỗi con thỏa Chuỗi_Mẫu trong Chuỗi_Văn_Bản thì trả về matchObject chứa thông tin chuỗi con này. Matches: Khi tìm thấy các chuỗi con thỏa Chuỗi_Mẫu trong Chuỗi_Văn_Bản thì trả về MatchCollection object chứa thông tin các chuỗi con này.

  6. Ví dụ: so sánh việc dùng regular expression // Nếu chỉ sử dụng thuật toán string zip = “1234C”; bool isGoodZip = true; foreach (char ch in zip) { if (!char.IsDigit(ch)) { isGoodZip = false; break; } } //dùng regular expression string searchString = “1234C”; string regExString = @”\d\d\d\d\d”; Regex rex = new Regex(regExString); bool isMatch = rex.IsMatch(searchString); Tạo object kiểu class Regex Gọi phương thức IsMatch //Cần US Zip code thì thay bằng: string searchString = “12345-6789”; string regExString = @”\d\d\d\d\d-\d\d\d\d”;

  7. Ví dụ: dùng match và matches Match "e" in testString Match "e" multiple times

  8. 2.

  9. Ví dụ: Dùng character class ".*" match nhóm ký tự bất kỳ trừ newline

  10. 3. Quantifier character class • Có thể dùng lớp ký tự định lượng. • Ví dụ: US Zip code: string regExString = @”\d{5}-\d{4}”; @ la chuoi thi \d neu khong phai \\d

  11. 3. Quantifier character class • Còn được gọi là metacharacter

  12. Ví dụ: Dùng static method Matches p xuất hiện 0 hoặc 1 lần

  13. 4. Greedy and lazy • Quantifier đều theo nguyên tắc greedy string testString = "123456789"; Console.Write("{0} \n", Regex.Match(testString,@"\d{5,10}")); • Nếu có ? sau ký tự quantifier nó trở nên lazy string testString = "123456789"; Console.Write("{0} \n", Regex.Match(testString,@"\d{5,10}?"));

  14. Ví dụ: greedy & lazy find sequences of word characters find sequences of word characters- lazy

  15. 5. Group, Optional Ngoài ra còn có một số character class khác:

  16. Ví dụ: subtraction character (khác với range character)

  17. Ví dụ: validating input data

  18. private void OkButton_Click(object sender, EventArgs e) { // find blank TextBoxes and order by TabIndex var emptyBoxes = from Control currentControl in Controls where currentControl is TextBox let box = currentControl as TextBox where string.IsNullOrEmpty( box.Text ) orderby box.TabIndex select box; // if there are any empty TextBoxes if ( emptyBoxes.Count() > 0 ) { MessageBox.Show( "Please fill in all fields","Missing Information", MessageBoxButtons.OK,MessageBoxIcon.Error ); emptyBoxes.First().Select(); } // end if else { // check for invalid input Ví dụ: validating input data

  19. Ví dụ: validating input data

  20. Ví dụ: validating input data Bắt đầu là ký tự hoa, rồi nhóm ký tự chữ cái, rồi có thể lặp lại nhiều lần khoảng trắng rồi ký tự hoa và nhóm ký tự chữ cái. Bắt đầu nhóm ký tự số, rồi khoảng trắng, rồi nhóm ký tự chữ cái hoặc nhóm ký tự chữ cái rồi khoảng trắng rồi nhóm ký tự chữ cái.

  21. Ví dụ: validating input data

  22. Ví dụ: validating input data định dạng gồm các ký số theo mẫu xxx-xxx-xxxx với ký số đầu nhóm khác 0, Lưu ý ^ và $

  23. Ví dụ: validating input data

  24. Ví dụ: validating input data

  25. Ví dụ: validating input data

  26. 6. Phương thức Replace, Split String Regex.Replace(String input,String pattern,String replacement) Trả về chuỗi kết quả là chuỗi sau khi đã thay chuỗi con tìm thấy đúng pattern trong input bằng chuỗi replacement. String[] Regex.Split(String input, String pattern) Trả về các chuỗi con được split từ chuỗi input tại các vị trí chuỗi con thỏa pattern.

More Related