Загрузка данных
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace RegexValidation
{
// =========================
// 1.1 Проверка пароля
// =========================
class PasswordValidator
{
public static bool IsValid(string password)
{
// Минимум 8 символов
if (password.Length < 8)
return false;
// Хотя бы 1 большая буква
if (!Regex.IsMatch(password, @"[A-Z]"))
return false;
// Хотя бы 1 маленькая буква
if (!Regex.IsMatch(password, @"[a-z]"))
return false;
// Хотя бы 1 цифра
if (!Regex.IsMatch(password, @"\d"))
return false;
// Минимум 2 спецсимвола
if (Regex.Matches(password, @"[!$%@#&*?]").Count < 2)
return false;
// Нет одинаковых подряд
if (Regex.IsMatch(password, @"(.)\1"))
return false;
return true;
}
}
// =========================
// 1.2 Проверка цвета
// =========================
class ColorValidator
{
public static bool IsValid(string color)
{
// HEX
if (Regex.IsMatch(color, @"^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"))
return true;
// RGB
if (Regex.IsMatch(color, @"^rgb\(\d{1,3},\d{1,3},\d{1,3}\)$"))
return true;
// HSL
if (Regex.IsMatch(color, @"^hsl\(\d{1,3},\d{1,3}%?,\d{1,3}%?\)$"))
return true;
return false;
}
}
// =========================
// 1.3 Проверка даты
// =========================
class DateValidator
{
public static bool IsValid(string date)
{
// Форматы:
// 14.09.2023
// 14/09/2023
// 2023-09-14
// September 14, 2023
string pattern1 = @"^\d{1,2}[./-]\d{1,2}[./-]\d{4}$";
string pattern2 = @"^\d{4}[./-]\d{1,2}[./-]\d{1,2}$";
string pattern3 = @"^[A-Za-z]+\s\d{1,2},\s\d{4}$";
return Regex.IsMatch(date, pattern1) ||
Regex.IsMatch(date, pattern2) ||
Regex.IsMatch(date, pattern3);
}
}
// =========================
// 1.4 Работа с текстом
// Вариант 7
// =========================
class TextProcessor
{
private string text;
public TextProcessor(string path)
{
text = File.ReadAllText(path);
}
// Сколько предложений начинается со слова
public int CountSentences(string word)
{
string[] sentences = text.Split('.', '!', '?');
int count = 0;
foreach (string sentence in sentences)
{
string trimmed = sentence.Trim();
if (trimmed.StartsWith(word,
StringComparison.OrdinalIgnoreCase))
{
count++;
}
}
return count;
}
}
// =========================
// Главная программа
// =========================
class Program
{
static void Main()
{
// ===== 1.1 =====
Console.WriteLine("Проверка пароля:");
Console.WriteLine(
PasswordValidator.IsValid("Aa1!!test")
);
// ===== 1.2 =====
Console.WriteLine("\nПроверка цвета:");
Console.WriteLine(
ColorValidator.IsValid("#fff")
);
// ===== 1.3 =====
Console.WriteLine("\nПроверка даты:");
Console.WriteLine(
DateValidator.IsValid("14.09.2023")
);
// ===== 1.4 =====
Console.WriteLine("\nАнализ текста:");
string path = "input.txt";
if (File.Exists(path))
{
TextProcessor processor =
new TextProcessor(path);
int result =
processor.CountSentences("Информатика");
Console.WriteLine(
$"Количество предложений: {result}"
);
}
else
{
Console.WriteLine("Файл input.txt не найден");
}
}
}
}