-2

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})

Could someone please explain these qualifiers briefly.

I got this Pattern from http://www.mkyong.com/regular-expressions/how-to-validate-password-with-regular-expression/

What does the dot mean

And how come the + qualifier is not used when it must occur at least one time

Also what qualifier should be used for zero or more times.

I'm new to this pattern matching in java, it's kind of hard to grasp

Marc
  • 29
  • 5

2 Answers2

0

Ok, let's break it up a little: firstly, '.' = anything

(

(?=.*\d) --- '?=' is regex 'look-ahead' - this asserts that there is a \d (digit) somewhere in the string

(?=.*[a-z]) --- 'look-ahead' again - this asserts that there is a [a-z] (lower-case letter) somewhere in the string

(?=.*[A-Z]) --- 'look-ahead' again - this asserts that there is a [A-Z] (upper-case letter) somewhere in the string

(?=.*[@#$%]) ---- yet another 'look-ahead' - asserts that there is at least one of the character class defined as @,#,$,%

.{6,20} - this asserts that the string must contain between 6 and 20 symbols of 'anything' i.e. '.'

)

Hope it helps! If there's still anything unclear, please simply say so.

d'alar'cop
  • 2,357
  • 1
  • 14
  • 18
  • 1
    The dot is [_almost_ anything](http://www.regular-expressions.info/dot.html)... – Boris the Spider Apr 12 '13 at 23:26
  • Ah yes, the dot is indeed anything except the newline character - unless in the language you are using there is a flag or modifier to make it match _anything_... Anyway, if the answer is sufficient for you please go ahead and tick it :) – d'alar'cop Apr 12 '13 at 23:30
  • @d'alar'cop: Oh, no. It doesn't **just** exclude new line character in Java and JavaScript: http://stackoverflow.com/questions/14648743/whats-the-difference-between-these-regex/14648811#14648811. And the max/min length is only true with `String.matches()` or `Matcher.matches()` – nhahtdh Apr 13 '13 at 06:00
  • @nhahtdh ah, interesting. thanks mate. I'll keep that in mind – d'alar'cop Apr 13 '13 at 06:18
0

You don't need the + quantifier, because you are only looking ahead to see "at least one" of everything. (a number, a lowercase, an uppercase, a symbol, and at least six to twenty total chars)

The . means "anything". Matches any character, except newline (usually).

Scott Weaver
  • 7,192
  • 2
  • 31
  • 43