Solved

Dissecting The following  Regular Express

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now