Regular Expression Password Validation - regex

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

Related

Matching consecutive characters with password limitation

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.

combinig regex partly using | not working c++

so this is going to be a regex for email address.
username: capital and small letters, digits, and underscore and dot
^[A-Za-z0-9._]+
then there's # and domain: capital and small letters and digits
#[A-Za-z0-9]+
then there's dot and tld: at least 2 characters (letters and digits) and can have maximum one dot.
I used | to have both at least 2 characters and maximum one dot:
([A-Za-z0-9]{2,}|[.]{0,1})
so the complete regex is this:
regex reg ("^[A-Za-z0-9._]+#[A-Za-z0-9]+\\.([A-Za-z0-9]{2,}|[.]{0,1})$");
but the maximum one dot rule isn't working. when I input zohal#gmail.df.g (not real of course) it gives false. it does work in other cases like zohal#gmail.com though.
You may use
regex reg(R"(^[A-Za-z0-9._]+#[A-Za-z0-9]+(?:\.[A-Za-z0-9]+)+$)")
See the regex demo
Details
^ - start of string
[A-Za-z0-9._]+ - 1 or more letters, digits, . or _
# - a # char
[A-Za-z0-9]+ - 1 or more letters or digits
(?:\.[A-Za-z0-9]+)+ - 1 or more occurrences of a dot and then 1 or more letters or digits
$ - an end of string position.
Since there are two + quantified patterns after #, you do not need an explicit (?=(?:[^A-Za-z0-9]*[A-Za-z0-9]){2}) lookahead to require at least two letters or digits.

Regular Expression containing letters and spaces in specified fashion

I am working on a text processing Api in java. I need to match the strings which are:
At least 8 characters in length.
Should only contain uppercase letters, lowercase letters or spaces.
Spaces should not be present in between the letters. They can however be leading or trailing. The String can also contain only spaces which are at least 8.
Regular expression which I tried but failed:
^\s*[a-zA-Z]{8,}\s*$
Demo of my tries in here.
Any help will be welcomed.
You can use the below regex to achieve your result:
^(?=.{8,}) *[a-zA-Z]* *$
Explanation of the above regex:
^ - denotes start of the test String.
(?=) - Positive lookahead.
.{8,} - any character other than newline with length at least 8.
* - 0 or more spaces in order to match the leading spaces.(\s is avoided)
[a-zA-Z]* - 0 or more letters (uppercase or lowercase). (You can use [a-z]* along with i(case insensitive) flag. Although, there will be no effect on performance.)
* - 0 or more spaces in order to match the trailing spaces.(\s is avoided)
$ - denotes end of the test String.
Above regex demo.

Regular expression limit length of character while not matching last character of string

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.

Regular expression - starting with 3 alphanumeric characters which includes at least one letter and one number, and ending with a letter

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