Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dissecting The following  Regular Express

Posted on 2011-09-14
3
Medium Priority
?
383 Views
Last Modified: 2012-05-12
Hi All,

I have found the following regular expression password validation pattern which validates the following:

Must have as least 1 number
Must have at least 1 special character
Must have more than 10 characters

"(?=^.{10,}$)(?=.*\d)(?=.*\W+)(?![.\n]).*$"

Could someone break the expression down for me and let me know which part applies to which validation rule above so that I get a better understanding of regexp?

Many thanks,

Rit
0
Comment
Question by:rito1
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 1000 total points
ID: 36538935
Must have as least 1 number
(?=.*\d)

Must have at least 1 special character
(?=.*\W+)

Must have more than 10 characters
(?=^.{10,}$)

One more requirement not listed: Must not have a 'new line' char
(?![.\n])


I hope this helps.
0
 
LVL 13

Assisted Solution

by:Carl Bohman
Carl Bohman earned 1000 total points
ID: 36539196
Actually, that last one is more like: Must not start with with either a period or a new line character.  If you really want to ensure that no periods or newline characters exist, you need to add .* like the others: (?!.*[.\n])  If that period was supposed to be part of the .* and the expression was supposed to exclude only newline characters, then you want this: (?!.*\n)

Newline characters, whitespace, the null character ('\0'), and some other potentially troublesome characters are allowed.  Note that newline, whitespace, etc. all match the \W, which is supposed to be a test for special characters, but is really a test for any character other than [a-zA-Z0-9_].  Depending on the application, this may be a non-issue, but it's something to be aware of.

As a style comment, I would recommend moving the carat (^) outside of the first lookahead to make it more clear that all of the tests are anchored at the start of the string: ^(?=.{10,}$)(?=.*\d)(?=.*\W+)(?![.\n]).*$
0
 
LVL 1

Author Closing Comment

by:rito1
ID: 36541250
HI Both,

Thank you for the very helpful response. Its much appreciated.

Rit
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question