How do you know that it compiles to the latter string? If you're using Quickwatch to view the string at runtime, you will see escaped strings.
On Aug 17, 8:25 pm, xzzy <[email protected]> wrote: > @"^.*(?=.{10,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=...@#$%^&+=]).*$" > > compiles to: > > @"^.*(?=.{10,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=...@#$%^&+=]).*$" > > = the compiler is adding an extra backslash in front of \d)(? > I don't know how to prevent this from happening > > On Aug 17, 12:15 am, Cerebrus <[email protected]> wrote: > > > > > 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- Hide quoted text - > > - Show quoted text -
