-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}$");
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
BigRabbit
  • 79
  • 1
  • 8

2 Answers2

5

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

LegionMammal978
  • 680
  • 8
  • 15
-1

it seems you have one space after ?

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

remove that.

Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31