Strong password check - regex

I need a regular expression that checks if passwords:
are at least 8 characters
contain at least 1 uppercase letter
contain at least 1 lowercase letter
contain at least 1 number or at least 1 special character
do not repeat a character more than 2 times in row
Examples that should match:
Test1234!
Te123stE
Examples that should not match:
Teeest123!
!TESTT1234
This is what I tried so far:
^(?!.*pass|.*Pass|.*qwer|.*Qwer)(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9#?!#$%^&*-]).{8,}$
I don't know how to match the repeating characters. Any ideas?

If you want to fail the match if there are 3 or more consecutive identical chars, use (?!.*(.)\1{2}) lookahead:
^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9#?!#$%^&*-]*[0-9#?!#$%^&*-])(?!.*[pP]ass|.*[qQ]wer)(?!.*(.)\1{2}).{8,}$
See the regex demo.
If you want to fail the match if there are 3 or more not necessarily consecutive identical chars, use (?!.*(.).*\1.*\1) lookahead:
^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9#?!#$%^&*-]*[0-9#?!#$%^&*-])(?!.*[pP]ass|.*[qQ]wer)(?!.*(.).*\1.*\1).{8,}$
See this regex demo
NOTE the changes: the most efficient lookaheads are moved to the beginning (right after ^) and the least efficient are moved to the right.
The most common lookaheads that check lower, upper case letters and special/digit chars are enhanced according to the principle of contrast.
Pattern details
^ - start of string
(?=[^A-Z]*[A-Z]) - there must be at least one uppercase ASCII letter
(?=[^a-z]*[a-z]) - there must be at least one lowercase ASCII letter
(?=[^0-9#?!#$%^&*-]*[0-9#?!#$%^&*-]) - there must be at least one special char from the 0-9#?!#$%^&*- set (digit or #, ?, !, #, $, %, ^, &, *, -)
(?!.*[pP]ass|.*[qQ]wer) - no pass/Pass and qwer/Qwer in the string allowed
(?!.*(.).*\1.*\1) - no three not necessarily consecutive repeating chars allowed
.{8,} - eight or more chars other than line break chars
$ - end of string.

You can use the following regular expression:
/
(?=.*[0-9#?!#$%^&*-]) # 1+ digit or special char
(?=.*[A-Z]) # 1+ uc letters
(?=.*[a-z]) # 1+ lc letters
(?=[a-zA-Z0-9#?!#$%^&*-]*$) # all chars valid
(?!.*(.)\1{2}) # no 3+ repeats
.{8,} # match string if length >= 8
/x # free-spacing mode
Demo

Related

Regex for combination of alphnumeric letters which has at least 2 uppercase letter or 1 number?

I need a regex for combination of numbers and uppercase letters and maybe lowercase letters and /,- characters, which contains at least 4 characters.
But of course it should contain at least 2 uppercase letter or one number.
I tried this:
barcode_regex = r"(?=(?:.+[A-Z]))(?=(?:.+[0-9]))([a-zA-Z0-9/-]{4,})"
For example match such cases as follows:
ametFXUT0
G197-6STK
adipiscXWWFHH
A654/9023847
HYJ/54GFJ
hgdy67h
You could use a single lookahead to assert at least 4 characters, and the match either a single digit or 2 uppercase chars in the allowed ranges.
^(?=.{4})(?:[A-Za-z/,-]*\d|(?:[a-z\d/,-]*[A-Z]){2})[A-Za-z\d/,-]*$
Explanation
^ Start of string
(?=.{4}) Assert 4 charcters
(?: Non capture group
[A-Za-z/,-]*\d Match optional allowed characters without a digit, then match a digit
| Or
(?:[a-z\d/,-]*[A-Z]){2} Match 2 times optional allowed characters withtout an uppercase char, then match an uppercase char
) Close non capture group
[A-Za-z\d/,-]* Match optional allowed characters
$ End of string
See a regex demo.
You could use two lookaheads combined via an alternation to check for 2 uppercase or 1 number:
^(?:(?=.*[A-Z].*[A-Z])|(?=.*\d))[A-Za-z0-9/-]+$
Demo
This regex patterns says to:
^
(?:
(?=.*[A-Z].*[A-Z]) assert that 2 or more uppercase are present
| OR
(?=.*\d) assert that at least one digit is present
)
[A-Za-z0-9/-]+ match any alphanumeric content (plus forward slash or dash)
$

How to write regex to prevent passwords with consecutive characters?

I have to check the validation of a password that must have at least 3 capital letters, 3 lower case letters, 2 digits, at least one of those characters (!##$*), and the trickiest one it can not have the same character in a row. For example, "beer" is not allowed.
That's what I have done but it doesn't do a lot:
(?=[0-9]{2})&(?=[a-z]{3})&(?=[A-Z]{3})&(?=[!##$*])&(?:(?!.([a-z]|[0-9]|[A-Z]|[!##$*]{2})))
You may use the following pattern:
^(?=(?:.*[A-Z]){3})(?=(?:.*[a-z]){3})(?=(?:.*[0-9]){2})(?=.*[!##$*])(?!.*(.)\1).*$
Demo.
Breakdown:
^ - Beginning of string.
(?=(?:.*[A-Z]){3}) - A positive Lookahead to assert at least 3 capital letters.
(?=(?:.*[a-z]){3}) - A positive Lookahead to assert at least 3 lowercase letters.
(?=(?:.*[0-9]){2}) - A positive Lookahead to assert at least 2 digits.
(?=.*[!##$*]) - A positive Lookahead to assert at least one symbol.
(?!.*(.)\1) - A negative Lookahead to reject the same character repeated twice in a row.
.*$ - Match zero or more characters until the end of the string.
Note: If you want to prevent the user from using any additional characters, you may replace the final .* part with:
[A-Za-z0-9!##$*]*

RegEx: How to match a whole string with fixed-length region with negative look ahead conditions that are overriden afterwards?

The strings I parse with a regular expression contain a region of fixed length N where there can either be numbers or dashes. However, if a dash occurs, only dashes are allowed to follow for the rest of the region. After this region, numbers, dashes, and letters are allowed to occur.
Examples (N=5, starting at the beginning):
12345ABC
12345123
1234-1
1234--1
1----1AB
How can I correctly match this? I currently am stuck at something like (?:\d|-(?!\d)){5}[A-Z0-9\-]+ (for N=5), but I cannot make numbers work directly following my region if a dash is present, as the negative look ahead blocks the match.
Update
Strings that should not be matched (N=5)
1-2-3-A
----1AB
--1--1A
You could assert that the first 5 characters are either digits or - and make sure that there is no - before a digit in the first 5 chars.
^(?![\d-]{0,3}-\d)(?=[\d-]{5})[A-Z\d-]+$
^ Start of string
(?![\d-]{0,3}-\d) Make sure that in the first 5 chars there is no - before a digit
(?=[\d-]{5}) Assert at least 5 digits or -
[A-Z\d-]+ Match 1+ times any of the listed characters
$ End of string
Regex demo
If atomic groups are available:
^(?=[\d-]{5})(?>\d+-*|-{5})[A-Z\d_]*$
^ Start of string
(?=[\d-]{5}) Assert at least 5 chars - or digit
(?> Atomic group
\d+-* Match 1+ digits and optional -
| or
-{5} match 5 times -
) Close atomic group
[A-Z\d_]* Match optional chars A-Z digit or _
$ End of string
Regex demo
Use a non-word-boundary assertion \B:
^[-\d](?:-|\B\d){4}[A-Z\d-]*$
A non word-boundary succeeds at a position between two word characters (from \w ie [A-Za-z0-9_]) or two non-word characters (from \W ie [^A-Za-z0-9_]). (and also between a non-word character and the limit of the string)
With it, each \B\d always follows a digit. (and can't follow a dash)
demo
Other way (if lookbehinds are allowed):
^\d*-*(?<=^.{5})[A-Z\d-]*$
demo

checking if one expression contains the next expression in regex

I want my regex to allow alphanumeric characters, "/_-" and white spaces in between but it must always have at least one alphanumeric character.
my validation goes like this,
/^([A-Za-z0-9/-]+[A-Za-z0-9/-\s]*[A-Za-z0-9/_-]+)$/
It should accept **ABC_1-2-3 but it must not allow 123 or -_/ alone
Can somebody help me please.
The below given regex will capture strings with alpha-numeric characters with optional white space, hyphen and underscore in it. Try it.
([*A-Za-z]+(\s+)?([\d\-_]+)?)
Your regex is almost right, you need to add 2 positive lookaheads at the start to require at least 1 letter and at least 1 digit:
/^(?=.*[a-z])(?=.*\d)[a-z0-9\/_-][a-z0-9\/_\s-]*[a-z0-9\/_-]$/i
See the regex demo (in the demo, \s is replaced with a space since the demo is multiline).
Details:
^ - start of string
(?=.*[a-z]) - after any 0+ chars other than line break chars, there must be at least 1 letter (replace .* with [^a-z]* for better performance)
(?=.*\d) - after any 0+ chars other than line break chars, there must be at least 1 digit (replace.with\D` for better performance)
[a-z0-9\/_-] - a letter, digit, /, _ or -
[a-z0-9\/_\s-]* - 0+ letters, digits, /, whitespaces, _ or -
[a-z0-9\/_-] - a letter, digit, /, _ or -
$ - end of string.
The i modifier makes the pattern case insensitive.

Password validation using regular expressions

I am trying to validate a password with the following rules:
Must have at least eight characters.
Must contain ONLY letters and digits.
Must contain at least two digits.
So far I wrote this code:
[0-9a-zA-Z] (?=(.*\d){2}) {8,}
Im not sure why the passwords I enter returns invalid although it follows the rules.
Remember that spaces are meaningful in a regex pattern, so you require at least 8 spaces at the end. There are no anchors in the regex, so the length limitation might not work even if you write a correct pattern. So far, this will match an alphanumeric, a space that is followed with 2 occurrences of any 0+ chars followed with a digit, but since there is space with {8,} quantifier, this pattern will never match anything.
You need
^(?=.{8})[a-zA-Z]*(?:\d[a-zA-Z]*){2}[a-zA-Z0-9]*$
See the regex demo
^ - start of string
(?=.{8}) - at least 8 chars
[a-zA-Z]* - zero or more letters
(?:\d[a-zA-Z]*){2} - 2 sequences of:
\d - a digit (may be replaced with [0-9])
[a-zA-Z]* - zero or more letters
[a-zA-Z0-9]* - 0+ alphanumeric chars
$ - end of string.
Alternatively, you may use
^(?=(?:[a-zA-Z]*\d){2})[a-zA-Z0-9]{8,}$
See another regex demo
Here, (?=(?:[a-zA-Z]*\d){2}) will require at least 2 occurrences of 0+ letters followed with a digit in the string and [a-zA-Z0-9]{8,} will match 8 or more alphanumeric chars.