Solved

Dissecting The following  Regular Express

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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…
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…
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…

739 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