
Practice 50 C# Regular Expression Programming Questions
Q1. Write a C# program to check if a given string matches a valid email address pattern using regular expressions.
Input: email = "test@example.com"
Output: Valid Email
Q2. Write a C# program to check if a given string contains a valid phone number (xxx-xxx-xxxx) using regular expressions.
Input: phone = "123-456-7890"
Output: Valid Phone Number
Q3. Write a C# program to validate if a given string is a valid date (dd/mm/yyyy) using regular expressions.
Input: date = "31/12/2025"
Output: Valid Date
Q4. Write a C# program to extract all the email addresses from a given text using regular expressions.
Input: text = "Please contact us at support@example.com or info@example.com"
Output: support@example.com, info@example.com
Q5. Write a C# program to check if a string contains a valid URL using regular expressions.
Input: url = "https://www.example.com"
Output: Valid URL
Q6. Write a C# program to check if a string contains only alphabets (uppercase and lowercase) using regular expressions.
Input: text = "HelloWorld"
Output: Valid Alphabet String
Q7. Write a C# program to check if a string contains only digits using regular expressions.
Input: text = "123456"
Output: Valid Digit String
Q8. Write a C# program to check if a string starts with a specific word (e.g., "Hello") using regular expressions.
Input: text = "Hello World"
Output: Matches
Q9. Write a C# program to check if a string ends with a specific word (e.g., "World") using regular expressions.
Input: text = "Hello World"
Output: Matches
Q10. Write a C# program to check if a string contains only alphanumeric characters using regular expressions.
Input: text = "abc123"
Output: Valid Alphanumeric String
Q11. Write a C# program to extract all the words starting with a capital letter from a given string using regular expressions.
Input: text = "Hello World, This is a Test"
Output: Hello, World, This, Test
Q12. Write a C# program to replace all digits in a string with the character 'X' using regular expressions.
Input: text = "My phone number is 123456"
Output: My phone number is XXXXXX
Q13. Write a C# program to validate a password using regular expressions (at least 8 characters, at least one uppercase letter, one lowercase letter, one digit, and one special character).
Input: password = "Password123!"
Output: Valid Password
Q14. Write a C# program to extract all the dates in the format dd/mm/yyyy from a given text using regular expressions.
Input: text = "The event is on 12/05/2025, and the last date for registration is 01/06/2025"
Output: 12/05/2025, 01/06/2025
Q15. Write a C# program to find all words that are exactly 5 characters long using regular expressions.
Input: text = "This is an example sentence"
Output: example
Q16. Write a C# program to match any word that contains a specific substring (e.g., "test") using regular expressions.
Input: text = "This is a test sentence"
Output: test
Q17. Write a C# program to remove all non-alphanumeric characters from a string using regular expressions.
Input: text = "Hello@World!123"
Output: HelloWorld123
Q18. Write a C# program to extract the first word from a given string using regular expressions.
Input: text = "Hello World"
Output: Hello
Q19. Write a C# program to validate if a string matches a valid hexadecimal color code using regular expressions.
Input: color = "#a3c113"
Output: Valid Hexadecimal Color Code
Q20. Write a C# program to extract all the words starting with a vowel from a given string using regular expressions.
Input: text = "An example of a string"
Output: An, example, of, a
Q21. Write a C# program to check if a string contains a valid social security number (xxx-xx-xxxx) using regular expressions.
Input: ssn = "123-45-6789"
Output: Valid SSN
Q22. Write a C# program to validate if a string matches a valid credit card number using regular expressions.
Input: creditCard = "1234-5678-9876-5432"
Output: Valid Credit Card Number
Q23. Write a C# program to find all the email addresses in a given paragraph using regular expressions.
Input: text = "Contact me at email1@example.com or email2@example.com"
Output: email1@example.com, email2@example.com
Q24. Write a C# program to check if a string is a valid IPv4 address using regular expressions.
Input: ipAddress = "192.168.1.1"
Output: Valid IPv4 Address
Q25. Write a C# program to check if a string is a valid IPv6 address using regular expressions.
Input: ipAddress = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
Output: Valid IPv6 Address
Q26. Write a C# program to validate if a string matches a valid time format (hh:mm:ss) using regular expressions.
Input: time = "12:34:56"
Output: Valid Time
Q27. Write a C# program to find all the phone numbers in the format (xxx) xxx-xxxx using regular expressions.
Input: text = "Call me at (123) 456-7890 or (987) 654-3210"
Output: (123) 456-7890, (987) 654-3210
Q28. Write a C# program to replace all occurrences of a word in a string with another word using regular expressions.
Input: text = "Hello World, Hello Everyone", target = "Hello", replacement = "Hi"
Output: Hi World, Hi Everyone
Q29. Write a C# program to extract all the URLs starting with "http" from a given text using regular expressions.
Input: text = "Visit https://example.com or http://test.com"
Output: https://example.com, http://test.com
Q30. Write a C# program to check if a string is a valid ZIP code (5 digits or 9 digits with hyphen) using regular expressions.
Input: zipCode = "12345"
Output: Valid ZIP Code
Q31. Write a C# program to check if a string contains only uppercase letters using regular expressions.
Input: text = "HELLOWORLD"
Output: Valid Uppercase String
Q32. Write a C# program to check if a string contains only lowercase letters using regular expressions.
Input: text = "helloworld"
Output: Valid Lowercase String
Q33. Write a C# program to match a string that starts with a digit using regular expressions.
Input: text = "5start with number"
Output: Matches
Q34. Write a C# program to find the first instance of a word using regular expressions.
Input: text = "This is an example sentence"
Output: This
Q35. Write a C# program to validate a hexadecimal number using regular expressions.
Input: hex = "A4F3B2"
Output: Valid Hexadecimal Number
Q36. Write a C# program to match a string that contains at least one number.
Input: text = "abc123"
Output: Matches
Q37. Write a C# program to extract all numbers from a given string using regular expressions.
Input: text = "My numbers are 123 and 456"
Output: 123, 456
Q38. Write a C# program to find all the words that start with a specific letter (e.g., 'a') using regular expressions.
Input: text = "apple banana avocado animal"
Output: apple, avocado, animal
Q39. Write a C# program to check if a string is a valid MAC address using regular expressions.
Input: macAddress = "00:1A:2B:3C:4D:5E"
Output: Valid MAC Address
Q40. Write a C# program to validate a string as a valid username (alphanumeric, between 6 and 12 characters) using regular expressions.
Input: username = "user123"
Output: Valid Username
Q41. Write a C# program to match a string that contains only spaces using regular expressions.
Input: text = " "
Output: Matches
Q42. Write a C# program to match a string that contains no spaces using regular expressions.
Input: text = "NoSpacesHere"
Output: Matches
Q43. Write a C# program to check if a string matches a valid time in 24-hour format (hh:mm) using regular expressions.
Input: time = "14:30"
Output: Valid Time
Q44. Write a C# program to extract all the numbers from a string and add them together using regular expressions.
Input: text = "There are 5 apples and 3 oranges"
Output: Sum = 8
Q45. Write a C# program to replace all spaces in a string with underscores using regular expressions.
Input: text = "Hello World"
Output: Hello_World
Q46. Write a C# program to match any string with a minimum of 8 characters using regular expressions.
Input: text = "abcdefgh"
Output: Matches
Q47. Write a C# program to check if a string matches a valid HTML tag using regular expressions.
Input: tag = "<div>"
Output: Valid HTML Tag
Q48. Write a C# program to extract all the numbers in the format (xxx) xxx-xxxx using regular expressions.
Input: text = "My numbers are (123) 456-7890 and (987) 654-3210"
Output: (123) 456-7890, (987) 654-3210
Q49. Write a C# program to match a string that contains at least one uppercase letter using regular expressions.
Input: text = "Hello123"
Output: Matches
Q50. Write a C# program to check if a string matches a valid time in 12-hour format (hh:mm AM/PM) using regular expressions.
Input: time = "02:30 PM"
Output: Valid Time