I need to define a regular expression that accepts Alphanumeric and the following special characters: @#$%&*()-_+][';:?.,!
I've come up with:
string pattern = @"[a-zA-Z0-9@#$%&*+\-_(),+':;?.,![]\s\\/]+$";
But this doesn't seem to be working. Can someone please let me know what is missing?
The [] in the middle need to be escaped*:
\[\]
You also probably want to anchor the start of the string with a ^.
* Probably just the ] but I like to do both for balance.
When defining a character class, you will need to escape the closing bracket ] within, just like "^", "-" and the escaping sequence \ itself, which you have done correctly:
string pattern = @"[a-zA-Z0-9@#$%&*+\-_(),+':;?.,![\]\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