Принципы работы с регулярными выражениями в C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System; using System.Text.RegularExpressions; namespace RegexSample { class mainClass { static void Main() { string text = "Итак, поиск с заменой осуществляется с помощью метода Replace."; bool match = Regex.IsMatch(text, "(.*?)"); Console.WriteLine(match.ToString()); string textOut = Regex.Replace(text, "(.*?)", @"$1", RegexOptions.IgnoreCase); Console.WriteLine(textOut); string[] splitStr = Regex.Split(text, ">"); for (int i = 0; i < splitStr.Length; i++) { Console.WriteLine(splitStr[i]); } MatchCollection matches = Regex.Matches(text, "(.*?)", RegexOptions.IgnoreCase); Console.WriteLine("Совпадений: " + matches.Count.ToString()); for (int i = 0; i < matches.Count; i++) { Console.WriteLine("Совпадение " + i + ": " + matches[i].Value); } Console.ReadLine(); } } } |