1. Trang chủ
  2. » Thể loại khác

csharp regular expressions

5 96 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Regular Expression trong C#

    • Construct cho định nghĩa Regular Expression trong C#

    • Lớp Regex trong C#

    • Ví dụ 1

    • Ví dụ 2

    • Ví dụ 3

Nội dung

http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Regular Expression C# Một regular expression pattern mà so khớp với văn đầu vào .Net Framework cung cấp phương tiện regular expression mà cho phép so khớp với Trong C#, pattern gồm nhiều ký tự, toán tử, construct Construct cho định nghĩa Regular Expression C# Có nhiều loại ký tự, toán tử construct đa dạng mà giúp bạn định nghĩa Regular Expression C# Bạn theo link sau để có chi tiết construct  Character escape  Lớp Character  Anchor  Grouping construct  Quantifier  Backreference construct  Alternation construct  Substitution  Miscellaneous constructs Lớp Regex C# Lớp Regex C# sử dụng để biểu diễn Regular Expression Nó có phương thức sử dụng phổ biến sau: STT Phương thức public bool IsMatch(string input) Chỉ có hay khơng Regular Expression cho Regex constructor tìm http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com thấy match chuỗi đầu vào xác định public bool IsMatch(string input, int startat) Chỉ có hay khơng Regular Expression cho Regex constructor tìm thấy match chuỗi đầu vào xác định, bắt đầu vị trí startat cho chuỗi public static bool IsMatch(string input, string pattern) Chỉ có hay khơng Regular Expression cho tìm thấy match chuỗi đầu vào xác định public MatchCollection Matches(string input) Tìm kiếm chuỗi đầu vào xác định tất xuất Regular Expression public string Replace(string input, string replacement) Trong chuỗi đầu vào xác định, thay tất chuỗi mà so khớp với Regular Expression pattern với chuỗi thay cho public string[] Split(string input) Chia chuỗi đầu vào thành mảng chuỗi phụ vị trí định nghĩa Regular Expression pattern xác định Regex constructor Để có danh sách đầy đủ phương thức thuộc tính, bạn vui lòng đọc Microsoft Documentation C# Ví dụ Ví dụ sau so khớp với từ mà bắt đầu với 'S': using System; using System.Text.RegularExpressions; http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com namespace RegExApplication { class Program { private static void showMatch(string text, string expr) { Console.WriteLine("The Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); foreach (Match m in mc) { Console.WriteLine(m); } } static void Main(string[] args) { string str = "A Thousand Splendid Suns"; Console.WriteLine("Matching words that start with 'S': "); showMatch(str, @"\bS\S*"); Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: Matching words that start with 'S': The Expression: \bS\S* Splendid Suns Ví dụ Ví dụ sau so khớp với từ mà bắt đầu với 'm' kết thúc với 'e': http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com using System; using System.Text.RegularExpressions; namespace RegExApplication { class Program { private static void showMatch(string text, string expr) { Console.WriteLine("The Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); foreach (Match m in mc) { Console.WriteLine(m); } } static void Main(string[] args) { string str = "make maze and manage to measure it"; Console.WriteLine("Matching words start with 'm' and ends with 'e':"); showMatch(str, @"\bm\S*e\b"); Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: Matching words start with 'm' and ends with 'e': The Expression: \bm\S*e\b make maze manage http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com measure Ví dụ Ví dụ sau thay white space: using System; using System.Text.RegularExpressions; namespace RegExApplication { class Program { static void Main(string[] args) { string input = "Hello World "; string pattern = "\\s+"; string replacement = " "; Regex rgx = new Regex(pattern); string result = rgx.Replace(input, replacement); Console.WriteLine("Original String: {0}", input); Console.WriteLine("Replacement String: {0}", result); Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: Original String: Hello World Replacement String: Hello World http://vietjack.com/ Trang chia sẻ học online miễn phí Page ... đầu với 'S': using System; using System.Text.RegularExpressions; http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com /csharp/ index.jsp Copyright © vietjack.com namespace... Trang chia sẻ học online miễn phí Page http://vietjack.com /csharp/ index.jsp Copyright © vietjack.com using System; using System.Text.RegularExpressions; namespace RegExApplication { class Program... miễn phí Page http://vietjack.com /csharp/ index.jsp Copyright © vietjack.com measure Ví dụ Ví dụ sau thay white space: using System; using System.Text.RegularExpressions; namespace RegExApplication

Ngày đăng: 02/12/2017, 20:06

TỪ KHÓA LIÊN QUAN

w