0

I have this regex expression but it accepting & symbol.

string REGEX_ALPHANUMERIC_UNDERSCORE_NOSPACE = "^[a-zA-Z0-9_-,!.]+$";

I am not getting why it is accepting & symbol. Please help anyone Thanks

Taylor
  • 113
  • 1
  • 1
  • 9
  • 1
    Are you saying you want to include characters `_` through to `,`? or that you want to include `_`, `-`, and `,`? – ProgrammingLlama Sep 06 '18 at 07:24
  • I want to include only these _ - , ! . – Taylor Sep 06 '18 at 07:25
  • 1
    At the moment C# is interpreting `_-,` as a range, and is actually throwing an exception for me: `[x-y] range in reverse order.` You need to escape the range character ( - ) as in malbarmawai's answer. – ProgrammingLlama Sep 06 '18 at 07:26
  • 1
    `[_-,]` denotes a _range_ from `_` to `,`. Usually, when `&` is accepted, this means that `&` is in the Unicode range between those two characters. For C# this may be different, or should throw an error, as John said. Just use `-` at the end of the character class to make it unambiguous, or escape it as `\-` or something similar. – Sebastian Simon Sep 06 '18 at 07:26
  • @Xufox It does not work, Could you please help in this – Taylor Sep 06 '18 at 07:38
  • @Taylor What does not work? We can't help with "it". – Ivar Sep 06 '18 at 08:53
  • 1
    @Taylor have you try this `"^[a-zA-Z0-9_\\-,!.]+$"` – Muhammed Albarmavi Sep 06 '18 at 09:29

1 Answers1

1

Try like this

string REGEX_ALPHANUMERIC_UNDERSCORE_NOSPACE = "^[a-zA-Z0-9_\\-,!.]+$";
Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91