Hello guys I'm trying to match the following regex:
Minimum characters: 8
Maximum characters: 22
Minimum uppercase letters: 1
Minimum lowercase letters: 1
Minimum digits: 2
Special characters are allowed
First character must be a letter
Maximum consecutive identical characters: 2
I've manage to complete every condition but the consecutive ones with:
(?=^.{8,22}$)(?=(.*\d){2})(?=(.*[A-Z]))^[a-zA-Z].*$
Following the post RegEx No more than 2 identical consecutive characters and a-Z and 0-9 I've seen that the way of not matching exact characters is:
((.)\2?(?!\2))+
But I'm unable to mix them both and have the full matching result. The tries are being done here: https://regex101.com/r/94KaXO/1/ where the first string should match but not the second one.
Thanks in advance.
You can use
^(?=.{8,22}$)(?!.*(.)\1{2})(?=(?:\D*\d){2})(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])[a-zA-Z].*$
See the regex demo.
Details:
^ - start of string
(?=.{8,22}$) - 8 to 22 chars other than line break chars are allowed in the string
(?!.*(.)\1{2}) - no 3 consecutive identical chars allowed anywhere after zero or more chars other than line break chars as many as possible
(?=(?:\D*\d){2}) - there must be at least 2 not necessarily consecutive digits in the string
(?=[^A-Z]*[A-Z]) - there must be at least one uppercase letter in the string
(?=[^a-z]*[a-z]) - there must be at least 2 one lowercase letter in the string
[a-zA-Z] - a letter
.* - zero or more chars other than line break chars, as many as possible
$- end of string.
Related
The following regex working as expected other than the case that it's not allowed that all characters are the same characters.
^(?=[A-Z0-9]+[ -]?[A-Z0-9]+)(?!([A-Z0-9])(?:\1|[ -]){5,10}).{5,10}$
here minimum is 5 characters and the maximum is 10 characters
11114 allowed its minimum length matched as 5 and one charcter is diff so not all same charcters
11111115 allowed as one charcter is different and its more than 5 charcter.
2222222 not allowed as all are same characters
222-22 not allowed as all are same charcters
111-3 allowed as length 5 and one character is different
444-45 allowed as length more than 5
1234565436 allowed as length with in range 5 to 10
There is no need to repeat range quantifier {5,10} multiple times as that makes changing this regex harder for other cases.
You may use this regex for this:
^(?=.{5,10}$)([A-Z0-9])(?!(?:[ -]?\1)+$)[A-Z0-9]*[ -]?[A-Z0-9]+$
RegEx Demo
RegEx Breakup:
^: Start
(?=.{5,10}$): Assert that we have 5 to 10 chars till end
([A-Z0-9]): Match a letter or digit and capture in group #1
(?!(?:[ -]?\1)+$): Negative lookahead to fail the match if same captured value is repeated till end
[A-Z0-9]*: Match 0 or more letter or digit
[ -]?: Match optional space or hyphen
[A-Z0-9]+: Match 1 or more letter or digit
$: End
I'm working on a new regex to prepare for a host-name on a virtual machine, however, I'm running into issue on how to limit character length of 24 while making sure the last character is not a dot or a minus. (the first character must be an alpha character)
I have gotten as far as making sure the first character is an alpha. The second group of characters are 23 in length with [a-zA-z0-9] including the dot and minus. I've tried the negative look-behind .+(?<!-|\.)$ in addition but does not work.
^[a-zA-Z]([a-zA-Z0-9-.]{0,23}
I expect the output of a123456789012345678911234 to be correct already.
I expect this output should be incorrect a12345678901234567891123-
You may use
^[a-zA-Z](?:[a-zA-Z0-9.-]{0,22}[a-zA-Z0-9])?$
See the regex demo and the regex graph:
Details
^ - start of string
[a-zA-Z] - a letter
(?:[a-zA-Z0-9.-]{0,22}[a-zA-Z0-9])? - an optional sequence of:
[a-zA-Z0-9.-]{0,22} - 0 to 22 letters, digits, . or - chars
[a-zA-Z0-9] - a letter or digit
$ - end of string.
In order to limit the number of characters, the expression must be enclosed in ^ and $ denoting the beginning and the end.
^[a-zA-Z][a-zA-Z0-9.-]{0,22}[a-zA-Z0-9]$
[] defines one character from those in parentheses
{a, b} defines the number of occurrences of the preceding character from 0 to 22 in the example
a total limit of 2 to 24 characters
This can be saved shorter but in this way it is the easiest to understand.
I'd like to use regular expression to validate the characters requirement of a password.
Requirement:
Password should have 16 characters.
Character 1-4 should have at least 1 digit.
Character 5-8 should have at least 1 lower case character.
Character 9-12 should have at least 1 upper case character.
Character 13-16 should have at least 1 symbol (punctuation).
I've tried to use regular expression with a positive lookahead but it does not work finally:
echo 'XXXX9999ccccXXX%' | grep -P '^((?=.*[0-9]).{4})((?=.*[a-z]).{4})((?=.*[A-Z]).{4})((?=.*\pP).{4})$'
Your lookahead syntax is off, because it is not correctly checking the positions you mentioned in your requirements. The following regex pattern seems to work for me:
^(?=.{0,3}\d)(?=.{4,7}[a-z])(?=.{8,11}[A-Z])(?=.{12,15}[.,$%^&!#]).{16}$
Explanation:
(?=.{0,3}\d) - number in positions 1-4
(?=.{4,7}[a-z]) - lowercase in positions 5-8
(?=.{8,11}[A-Z]) - uppercase in positions 9-12
(?=.{12,15}[.,$%^&!#]) - symbol in positions 13-16
Demo
I don't know grep or Linux well enough to comment on whether you are making best use, but this should at least fix any problems you were having with the pattern.
You may use
^(?=.{0,3}\d).{4}(?=.{0,3}[a-z]).{4}(?=.{0,3}[A-Z]).{4}(?=.{0,3}[\W_]).{4}$
See this demo
Basically, the pattern comprises four lookahead-consuming pattern parts, and since each consuming pattern matches 4 chars, in total, it matches string of 16 chars (note that ^ and $ anchors are also important).
Details
^ - start of string
(?=.{0,3}\d) - there must be a digit after 0 to 3 chars
.{4} - any 4 chars are consumed
(?=.{0,3}[a-z]) - there must be a lowercase letter after 0 to 3 chars
.{4} - any 4 chars are consumed
(?=.{0,3}[A-Z]) - there must be an uppercase letter after 0 to 3 chars
.{4} - any 4 chars are consumed
(?=.{0,3}[\W_]).{4} - there must be a special char (non-alphanumeric) after 0 to 3 chars
$ - end of string
I'm trying to make a regex that matches the following criteria:
4 characters.
The beginning 3 characters must be alphanumeric characters, including at least one letter and one digit.
The last character must be a letter.
So I expect the results would be:
case1: abcd -> no match
case2: 234d -> no match
case3: a23c -> match
case4: 3abc -> match
case5: xy23 -> no match
I tested the following regex which matches criteria 2, but still cannot find a solution to match criteria 1&3.
^(?!.*[^a-zA-Z0-9])(?=.*\d)(?=.*[a-zA-Z]).{3}$
I tried this one but it failed on case2.
^(?!.*[^a-zA-Z0-9])(?=.*\d)(?=.*[a-zA-Z]).{3}[a-zA-Z]$
How can I combine these criteria? Thanks!
You may use
^(?=.{0,2}[0-9])(?=.{0,2}[a-zA-Z])[0-9a-zA-Z]{3}[a-zA-Z]$
See the regex demo
Details
^ - start of string
(?=.{0,2}[0-9]) - there must be an ASCII digit after 0 to 2 chars
(?=.{0,2}[a-zA-Z])- there must be an ASCII letter after 0 to 2 chars
[0-9a-zA-Z]{3} - 3 ASCII alphanumerics
[a-zA-Z] - an ASCII letter
$ - end of string
No need to use complicated features for 3 or 4 characters:
/^(?:[a-z0-9](?:[0-9][a-z]|[a-z][0-9])|[0-9][a-z]{2}|[a-z][0-9]{2})[a-z]$/i
or
/^(?:[a-z](?:[0-9][a-z0-9]|[a-z][0-9])|[0-9](?:[a-z][a-z0-9]|[0-9][a-z]))[a-z]$/i
I'm trying to make a regex for allowing only strong passwords, strong in this case being defined as:
Must start with a letter (either uppercase or lowercase)
Must have at least 8 and up to 12 characters
Must have at least one uppercase letter
Must have at least three lowercase letters
Must have at least two numbers
Must have at least two special characters
Maximum number of identical consecutive characters is three
Now, last one is giving me trouble. How do I count consecutive characters?
For example, FOOfoo!?123 should work, but FOOOfoo!?12 should not (because or three esses).
What I've got so far:
^[A-Za-z]{1}(?=.*[A-Z]{1,})(?=.*[a-z]{3,})(?=.*[0-9]{2,})(?=.*[!?#*#&$]{2,}).{8,12}$
One more thing: something is amiss, because my regex above claims strings like FooFoo!?123 are invalid. I think it's because it only checks for one or more uppercase letters or three or more lowercase letters or numbers or specials, but I don't want that, I want that is the password contains three lowercase letters in total, it should be valid. How do I do that?
When you have so many conditions, it might be a good idea - provided your environment allows that - to split the regex and check each condition separately.
If you cannot do that, here is a free-spacing version of the fixed regex:
^ # start of string
(?=[^A-Z]*[A-Z]) # At least 1 uppercase ASCII letter
(?=(?:[^a-z]*[a-z]){3}) # at least 3 lowercase ASCII letters
(?=(?:[^0-9]*[0-9]){2}) # at least 2 ASCII digits
(?=(?:[^!?#*#&$]*[!?#*#&$]){2}) # at least 2 special symbols
(?!.*(.)\1{2}) # No 3 consecutive characters
[A-Za-z] # An ASCII letter
.{7,11} # 7 to 11 any characters but newline
$ # end of string
As a one-liner:
^(?=[^A-Z]*[A-Z])(?=(?:[^a-z]*[a-z]){3})(?=(?:[^0-9]*[0-9]){2})(?=(?:[^!?#*#&$]*[!?#*#&$]){2})(?!.*(.)\1{2})[A-Za-z].{7,11}$
See the regex demo
Notes:
Must have at least three lowercase letters and similar conditions are implemented using the principle of contrast, i.e. before [a-z], we may have 0+ opposite chars matched with [^a-z].
To match the 3 letters globally, not consecutively, we need to use a limiting quantifier on the grouping, not on the character class, thus, [a-z]{3,} (=consecutive 3 or more lowercase letters) is turned into (?:[^a-z]*[a-z]){3} (=3 sequences of non-lowercase letters followed with 1 lowercase letter).
The condition you needed is (?!.*(.)\1{2}) - a negative lookahead ((?!...)) that checks for the presence of any character captured with (.) that is repeated twice after it with the \1 backreference and {2} limiting quantifier set on the backreference. And .* means that the repeated characters may appear anywhere in the string.