-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
  • 94,086
  • 102
  • 195
  • 339
user2931871
  • 79
  • 1
  • 7

2 Answers2

5

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

LegionMammal978
  • 645
  • 9
  • 12
-1

it seems you have one space after ?

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

remove that.

Deepak Sharma
  • 4,079
  • 1
  • 13
  • 30