Загрузка данных


using System;

using System.IO;

using System.Text.RegularExpressions;

namespace RegexValidation

{

    // 1.1 Проверка пароля

    class PasswordValidator

    {

        public static bool IsValid(string password)

        {

            string pattern = @"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=(?:.*[!$%@#&*?]){2,})(?!.*(.)\1)[A-Za-z\d!$%@#&*?]{8,}$";

            return Regex.IsMatch(password, pattern);

        }

    }

    // 1.2 Проверка цвета

    class ColorValidator

    {

        public static bool IsValid(string color)

        {

            string rgb = @"^rgb\(\s*(\d{1,3}%?\s*,\s*){2}\d{1,3}%?\s*\)$";

            string hex = @"^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$";

            string hsl = @"^hsl\(\s*(360|3[0-5]\d|[12]?\d?\d)\s*,\s*(100|[1-9]?\d)%\s*,\s*(100|[1-9]?\d)%\s*\)$";

            return Regex.IsMatch(color, rgb) ||

                   Regex.IsMatch(color, hex) ||

                   Regex.IsMatch(color, hsl);

        }

    }

    // 1.3 Проверка даты

    class DateValidator

    {

        public static bool IsValid(string date)

        {

            string numericFormats =

                @"^(" +

                @"\d{1,2}[./-]\d{1,2}[./-]\d{4}|" +

                @"\d{4}[./-]\d{1,2}[./-]\d{1,2}" +

                @")$";

            string russianText =

                @"^\d{1,2}\s+(января|февраля|марта|апреля|мая|июня|июля|августа|сентября|октября|ноября|декабря)\s+\d{4}$";

            string englishText =

                @"^(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2},\s+\d{4}$";

            string shortEnglish =

                @"^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d{1,2},\s+\d{4}$";

            return Regex.IsMatch(date, numericFormats) ||

                   Regex.IsMatch(date, russianText, RegexOptions.IgnoreCase) ||

                   Regex.IsMatch(date, englishText, RegexOptions.IgnoreCase) ||

                   Regex.IsMatch(date, shortEnglish, RegexOptions.IgnoreCase);

        }

    }

    // 1.4 Обработка текста (вариант 7)

    class TextProcessor

    {

        private string text;

        public TextProcessor(string filePath)

        {

            text = File.ReadAllText(filePath);

        }

        // Сколько предложений начинается со слова "Информатика"

        public int CountSentencesStartingWithWord(string word)

        {

            string[] sentences = Regex.Split(text, @"(?<=[.!?])\s+");

            int count = 0;

            foreach (var sentence in sentences)

            {

                string trimmed = sentence.Trim();

                if (Regex.IsMatch(trimmed, $"^{word}\\b", RegexOptions.IgnoreCase))

                {

                    count++;

                }

            }

            return count;

        }

    }

    class Program

    {

        static void Main()

        {

            // ===== 1.1 =====

            Console.WriteLine("Проверка пароля:");

            Console.WriteLine(PasswordValidator.IsValid("aA1!!aaa"));

            // ===== 1.2 =====

            Console.WriteLine("\nПроверка цвета:");

            Console.WriteLine(ColorValidator.IsValid("#21f48D"));

            // ===== 1.3 =====

            Console.WriteLine("\nПроверка даты:");

            Console.WriteLine(DateValidator.IsValid("14.09.2023"));

            // ===== 1.4 (вариант 7) =====

            Console.WriteLine("\nАнализ текста:");

            string path = "input.txt";

            if (File.Exists(path))

            {

                TextProcessor processor = new TextProcessor(path);

                int result = processor.CountSentencesStartingWithWord("Информатика");

                Console.WriteLine($"Количество предложений: {result}");

            }

            else

            {

                Console.WriteLine("Файл input.txt не найден");

            }

        }

    }

}