Thursday, 15 August 2013

Validate string with a regular expression

Regular expression is really an amazing function in string processing. In this article, I will introduce a popular case using regular expression to solve validate string with muli-conditions: validate password strength.

Requirements
  • program language must support regular expression with look lookahead assertion.

These are the rules
  • The password length must be greater than or equal to 7 characters
  • The password must contain one or more uppercase characters
  • The password must contain one or more lowercase characters
  • The password must contain at least one numeric character.

And this is the answer
(?=^.{7,}$)(?=.*\d)(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$
You can see colors and very easy to know. The end to finish a regex (all of first statement is only condition, must have main body and check that end with end of line (with multiline mode) or end of string (with singleline mode).

Test online with


No comments:

Post a Comment