/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/fundamentals/runtime-libraries/snippets/System.Text.RegularExpressions/Regex/Overview/csharp/words.cs
50 строк
1 KB
Genevieve Warren
Add remarks for core types (#38970)
05 янв 2024, 22:14
Не верифицирован
05 янв 2024, 22:14
71d1377
Код
Авторство
О чём код?
// <snippet0> using System; using System.Text.RegularExpressions; public class Test { public static void Main () { // <snippet1> // Define a regular expression for repeated words. Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase); // </snippet1> // Define a test string. string text = "The the quick brown fox fox jumps over the lazy dog dog."; // <snippet2> // Find matches. MatchCollection matches = rx.Matches(text); // </snippet2> // <snippet3> // Report the number of matches found. Console.WriteLine("{0} matches found in:\n {1}", matches.Count, text); // </snippet3> // <snippet4> // Report on each match. foreach (Match match in matches) { GroupCollection groups = match.Groups; Console.WriteLine("'{0}' repeated at positions {1} and {2}", groups["word"].Value, groups[0].Index, groups[1].Index); } // </snippet4> } } // The example produces the following output to the console: // 3 matches found in: // The the quick brown fox fox jumps over the lazy dog dog. // 'The' repeated at positions 0 and 4 // 'fox' repeated at positions 20 and 25 // 'dog' repeated at positions 49 and 53 // </snippet0>