using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace ConsoleAdvancedTopics { class RegularExpressionEmail { // regex101.com tester // regexlib.com examples and patterns // Define the pattern to match // ^\w+@[a-zA-Z0-9_]+?\.[a-zA-Z0-9]{2,5}$ // ^ begins with $ end string \w word \d digit[0-9] // string pattern = @"^\w+@[a-zA-Z0-9_]+?\.[a-zA-Z0-9]{2,5}$"; private readonly Regex _emailRegex; // hidden public RegularExpressionEmail() // constructor name class name decoratored with a tuple () { // define the regex pattern for validation string pattern = @"^\w+@[a-zA-Z0-9_]+?\.[a-zA-Z0-9]{2,5}$"; _emailRegex = new Regex(pattern, RegexOptions.Compiled); } // constructor public bool IsValidEmail(string email) { return _emailRegex.IsMatch(email); } // IsValidEmail // List of test values public void ValidateEmails(string[] emails) { foreach (string email in emails) { bool isValid = IsValidEmail(email); Console.WriteLine($"Email: {email} is Valid: {isValid}"); } } // ValidateEmails } // RegularExpressionEmail }