I have below country names which contains some special characters.
So, I wrote a regular expression to validate that a country name that must not contain any numeric and special characters other than ,
.
'
-
(
)
I wrote below regex
string country = "COTE D’IVOIRE"
bool isValid = Regex.IsMatch(country.Trim(), @"^[a-zA-Z\-'()/.,\s]+$");
but it just not doing the thing. Can someone please let me know what I'm doing wrong.
If you plan to also allow curly apostrophe, just add it to the character class:
@"^[a-zA-Z’\-'()/.,\s]+$"
^
Note that you do not have to escape -
if it is at the end of the character class and you can use (?i)
case-insensitive modifier to shorten the a-zA-Z
part:
@"(?i)^[a-z’'()/.,\s-]+$"
C#:
string country = "COTE D’IVOIRE";
bool isValid = Regex.IsMatch(country.Trim(), @"(?i)^[a-z’'()/.,\s-]+$");
// or use RegexOptions.IgnoreCase option
//bool isValid = Regex.IsMatch(country.Trim(), @"^[a-z’'()/.,\s-]+$", RegexOptions.IgnoreCase);
Add the ’
to the list of symbols
string country = "COTE D’IVOIRE"
bool isValid = Regex.IsMatch(country.Trim(), @"^[a-zA-Z\-'’()/.,\s]+$");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With