I need to match these characters. This quote is from an API documentation (external to our company):
Valid characters: 0-9 A-Z a-z & # - . , ( ) / : ; ' @ "
I used this Regex
to match characters:
^[0-9a-z&#-\.,()/:;'""@]*$
However, this wrongly matches characters like %
, $
, and many other characters. What's wrong?
You can test this regular expression online using http://regexhero.net/tester/, and this regular expression is meant to work in both .NET and JavaScript.
You are not escaping the dash -
, which is a reserved character. If you add replace the dash with \-
then the regex no longer matches those characters between #
and \
Move the literal -
to the front of the character set:
^[-0-9a-z&#\.,()/:;'""@]*$
otherwise it is taken as specifying a range like when you use it in 0-9
.
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