In C#, you need to escape backslashes with another backslash. I
recommend the use of verbatim string literals for complicated strings
like this one. (prefix with "@").

On Aug 17, 3:42 am, xzzy <[email protected]> wrote:
> Thank you for any help with this problem with Regex and what it
> perceives to be an invalid escape sequence.
>
> (1) I use this Regex to validate a password:
> (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,10})(?=...@#$%^&+=]).*
> $     // Length: 6 to 10, letters & numbers
>
> it works, but it does not allow the use of: @#$%^&+=
>
> (2) So I tried: ^.*(?=.{10,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=...@#$%^&
> +=]).*$
> this works in Javascript,
> but in C#, the \d part of the statement does not compile: "Invalid
> escape sequence"
>
> (3) This compiles:
>    ^.*(?=.{10,})(?=.*" + "\\" + "d)(?=.*[a-z])(?=.*[A-Z])(?=...@#$%^&
> +=]).*$
> but does not work as it compiles to:
>    ^.*(?=.{10,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=...@#$%^&+=]).*$
> ( an extra backslash before \d)(?   )
>
> (4) So I tried: (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,10})(?=...@#
> $%^&+=])(?=...@#$%^&+=]).*$
> which is #1 above with ".*$" replaced with "(?=...@#$%^&+=]).*$"
> It compiles, but does not work, not even for as password as simple as:
> Aabbcc5

Reply via email to