Solved

Dissecting The following  Regular Express

Posted on 2011-09-14
3
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
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…

626 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