Advertisement
vvccs

EXP7_REGEX

Apr 13th, 2024 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. using System.Text.RegularExpressions;
  2. static void Main(string[] args)
  3. {
  4. string mobileRegex = @"^\d{10}$";
  5. string emailRegex = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
  6. string nameRegex = @"^[a-zA-Z\s]+$";
  7. bool exitRequested = false;
  8. while (!exitRequested)
  9. {
  10. Console.WriteLine("Choose what you want to validate:");
  11. Console.WriteLine("1. Name");
  12. Console.WriteLine("2. Email");
  13. Console.WriteLine("3. Mobile Number");
  14. Console.WriteLine("4. Exit");
  15.  
  16. int choice;
  17. if (!int.TryParse(Console.ReadLine(), out choice) || choice < 1 || choice > 4)
  18. {
  19. Console.WriteLine("Invalid choice. Please choose a number between 1 and 4.");
  20. continue;
  21. }
  22. string userInput;
  23. switch (choice)
  24. {
  25. case 1:
  26. Console.WriteLine("Enter name:");
  27. userInput = Console.ReadLine();
  28. Console.WriteLine("Name - Valid: " + Regex.IsMatch(userInput, nameRegex));
  29. break;
  30. case 2:
  31. Console.WriteLine("Enter email address:");
  32. userInput = Console.ReadLine();
  33. Console.WriteLine("Email - Valid: " + Regex.IsMatch(userInput, emailRegex));
  34. break;
  35. case 3:
  36. Console.WriteLine("Enter mobile number:");
  37. userInput = Console.ReadLine();
  38. Console.WriteLine("Mobile Number - Valid: " + Regex.IsMatch(userInput, mobileRegex));
  39. break;
  40. case 4:
  41. exitRequested = true;
  42. break;
  43. }
  44. }
  45. Console.WriteLine("Exiting program...");
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement