Sunday, February 22, 2009

RegExp on ASP.NET vs. The World

I was working on a Create New User page and had to add some Regular expressions to some of the fileds on the page. Everything gone well, everything but the regex for the password field.
My regex looked like that:
    (?=^\S[^<>])(?=\S*[0-9])(?=\S*[A-Z])\S{6,20}$

Which translates into: Accept every string of characters that doesn't have white space or '<' '>' and must have at least 1 numeric character and at least 1 upper-case letter and has to be between six to twenty characters long.
I usually test my regex with a nice free tool called: Regex-Coach.

However, when wiring it up on my aspx page, it translated into: Accept every string of characters that doesn't have white space or '<' '>' and must have at least 6 numeric characters and at least 6 upper-case letters.

After hitting my head on my desk for a while I decided to modify my regex to the following:
    (?=^\S[^<>]{5,19}$)(?=\S*[0-9])(?=\S*[A-Z])\S*$

Which translates into the same thing, however this time I moved the "at least 6-20 characters" restriction to the beginning of the expression. This regex worked on both the regex coach and on my aspx page. It seems a little odd to me that the same regex is being translated somehow differently after all regex is regex?!

No comments:

Post a Comment

 
Watch the latest videos on YouTube.com