What is the Regular expression for following constraint?

I want a regular expression for a string for password that contain at least three out of the following four types of characters:

  • English lowercase characters (a through z).
  • ENGLISH UPPERCASE CHARACTERS (A THROUGH Z).
  • Base 10 digits (0 through 9).
  • Non alphanumeric characters (e.g. !,$,#,%).

And should contain at least 8 character.

Answers


Given the monstrositya that these requirements are likely to generate, I suspect you're actually going to be better off doing this as multiple checks, something like (pseudo-code):

def check_password (s):
    if len(s) < 8:
        return false
    rules_followed = 0
    if match (s, "[a-z]") rules_followed++;
    if match (s, "[A-Z]") rules_followed++;
    if match (s, "[0-9]") rules_followed++;
    if match (s, "[!$#%]") rules_followed++;  # et cetera
    if rules_followed < 3:
        return false
    return true

This is likely to be much more readable when someone else has to maintain your code (or even if you have to maintain it six months from now).

Now I realise this may not always be possible (you may, for example, be stuck with a framework that only allows one regex for validation).

But, if it is possible, I strongly suggest you consider it. The first thing you should always try to optimise for is readability.


a You'll either end up with one of those massive lookahead regexes or a single regex containing the sixteen possibilities of ordering separated by |.

Neither of these are likely to be as efficient or readable as a simple code segment.


Need Your Help

View object in oracle adf

java sql oracle oracle-adf

I work with one application module which uses view object to manage database table. The problem occurs while this process:

Windows service to dismiss legal disclaimer

windows-xp

Is there a way push a keystroke to the legal text dialog using a windows service?