0

I have an api which accepts PIN which shouldnt be anything other than numeric. I am using regex to check if the pin contains any other character. This is my regex

var regex = new Regex("[a-zA-Z!@#$&(){}%^*_=;:?<>\\-`.+,/\"]");

I need to add verification for [] brackets also but whenever i put it inside the expression it doesn't work.

var regex = new Regex("[a-zA-Z!@#$&(){}%^*_=;:?<>[]\\-`.+,/\"]");

Do i need to use any special character for this ?

RISHABH
  • 65
  • 5
  • AFAIR, you need to escape them using \, just like you did for `"`. – Zohar Peled Jul 25 '18 at 07:17
  • There are many characters that are neither digits nor in your regex - why not just use `[0-9]` to match only digits? Or if you want to match invalid characters, use `[^0-9]` to match everything except those. – Jon Skeet Jul 25 '18 at 07:18
  • Escape `]` as you escaped the hyphen. – Wiktor Stribiżew Jul 25 '18 at 07:19
  • @Saud No need escaping `/` anywhere inside a .NET pattern, it is not a special char. Your suggestion is wrong, you escaped `[` that does not need escaping inside a character class, you should have escaped `]`. Well, if you tried your suggestion in code, you would get another syntax issue here, too. – Wiktor Stribiżew Jul 25 '18 at 07:56
  • @WiktorStribiżew StribiżewWoops! Confused it with PCRE. My bad. I've deleted my comment – Wololo Jul 25 '18 at 08:02
  • The solution is `"[][a-zA-Z!@#$&(){}%^*_=;:?<>\`.+,/\"-]"` – Wiktor Stribiżew Jul 25 '18 at 08:04

0 Answers0