Here is the .NET Regular Expression that I am using to create a strong password (which is not correct for my project password requirements):
(?=^.{15,25}$)(\d{2,}[a-z]{2,}[A-Z]{2,}[!@#$%&+~?]{2,})
Password requirements:
- Minimum 15 Character (up to 25)
- Two Numbers
- Two Uppercase Letters
- Two Lowercase Letters
- Two Special Characters
! @ # $ % & + ~ ?
They are not required to be beside one another & in the specific order as the Regular Expression that I pasted requires.
The above Regular Expression requires a password like this: 12abCD!@QWertyP
It REQUIRES them in the specific order in the RE... which is not what I want!
This should pass a correctly formatted RE with the specifications listed above: Qq1W!w2Ee#3Rr4@Tt5
How can I remove the necessity for them to be beside one another and in order?? Obviously the password should be random if the person so chooses.
I think you're looking for more than what a regex was designed to do.
Consider a C#/VB method like this:
I know this is a year old, but from the few responses, I gathered that this would work.
The top answer is correct except, it just needs a * preceding the pattern.
This will be much more readable and maintainable in classic code:
The pseudo-code would be:
It might be that you're implementing the requirements rather than in a position to influence them, but I'd generally recommend estimating the entrophy and using existing code to do that.
As far as I know, you cannot do that reasonably, meaning you'd have to list all possible order combinations in the regex, which would add up to 24 combinations.
I would do 4 separate checks:
\d{2,}
[a-z]{2,}
[A-Z]{2,}
[!@#$%&+~?]{2,}
Related question: Variable order regex syntax
As an aside, your rules look too cumbersome to me I would reconsider them, for example, to have 3 chars of two of etters, digits or symbols.
This regex will do what you want. It will be making up to 5 passes through your password string, but considering what you are doing with it, I don't expect that to be a problem.
Edited to fix a typo that ruined the regex.
I think it could be as follow too:
^(?=(.\d){2,})(?=(.[a-z]){2,})(?=(.[A-Z]){2,})(?=(.[!@#$%&+~?]){2,})).{15,25}$