-1

I'm trying to validate a password field using regex under the namespace System.Text.RegularExpressions but I'm getting three errors for

'Unrecognized escape sequence'.

When I double click on the errors it highlights the '-' in my expression for the character range but I don't know why this is wrong.

// password must contain one uppercase, one lowercase and one digit

Regex reg = new Regex("^(?=.*[!@#$%^&*()\-_=+`~\[\]{}?|])(?=.+[a-z])(?=.+[A-Z])(? =.+[0-9]).{8,50}$");
5
  • 1
    You are missing a @ before the regex literal. Nov 23, 2015 at 13:30
  • You should escape \ character. Nov 23, 2015 at 13:30
  • Why have you specified a maximum length for the password? Please, never do that.
    – David Arno
    Nov 23, 2015 at 13:30
  • 1
    Duplicate talks about paths, but it's the same \ character/lack of @ causing the problem. Nov 23, 2015 at 13:31
  • it seems (? =.+[0-9]).{8,50} here you have one space after ? it should not be Nov 23, 2015 at 13:31

2 Answers 2

5

Just add an @ before the first quote to make it a verbatim string literal or escape the backslashes as \\.

-1

it seems you have one space after ?

(? =.+[0-9]).{8,50}

remove that.

Not the answer you're looking for? Browse other questions tagged or ask your own question.