Solved

Dissecting The following  Regular Express

Posted on 2011-09-14
3
346 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 250 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 250 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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,…
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…

829 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