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.
Related
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
I have to match a string that is 3-6 characters long, contains at least one letter, but can have letters, numbers and only 1 "-".
The "-" must not be at the start or at the beginning.
Match:
string
str-ng
st-ng
s1-1g
st-1g
Do not match:
strings
-string
string-
st--ng
s-tn-g
1111
st
The closest I've gotten is this:
^((?!-.*-)[0-9A-Z]{3,6})$
But this divides the regex match with - So it matches s-tri but not st-ri because there aren't 3 chars at each end
Maybe you can use:
^(?=.*[a-z])(?!-|.*-$|.*-.*-)[a-z\d-]{3,6}$
See the online demo
^ - Start string anchor.
(?=.*[a-z]) - Positive lookahead to make sure there is at least one letter.
(?!-|.*-$|.*-.*-) - Negative lookahead to prevent a hyphen at the beginning or at the end or multiple.
[a-z\d-]{3,6} - Three to six times a character from the give class.
$ - End string anchor.
Note that I used the case-insensitive flag.
You can use
^(?=.{3,6}$)(?=[^a-zA-Z]*[A-Za-z])[0-9a-zA-Z]+(?:-[0-9a-zA-Z]+)?$
See the regex demo. Details:
^ - start of string
(?=.{3,6}$) - string must contain three to six chars other than line break chars
(?=[^a-zA-Z]*[A-Za-z]) - there must be at least one ASCII letter in the string
[0-9a-zA-Z]+ - one or more alphanumeric ASCII chars
(?:-[0-9a-zA-Z]+)? - an optional sequence of - and then one or more alphanumeric ASCII chars
$ - end of string.
Looking at the pattern that you tried, you meant to exclude the match when there are 2 hyphens present using the negative lookahead.
Also this part [0-9A-Z]{3,6} does not match a hyphen.
Reading
The "-" must not be at the start or at the beginning.
You might do that using
^(?![^\n-]*-[^\n-]*-)(?=[^a-zA-Z\n]*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9-]{2,5}$
Regex demo
If you meant also no - at the end:
^(?![^\n-]*-[^\n-]*-)(?=[^a-zA-Z\n]*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9-]{1,4}[a-zA-Z0-9]$
Explanation
^ Start of string
(?![^\n-]*-[^\n-]*-) Assert not 2 times -
(?=[^a-zA-Z\n]*[a-zA-Z]) Assert a char a-zA-Z
[a-zA-Z0-9] Match One of the listed without -
[a-zA-Z0-9-]{1,4} Repeat 1-4 times any of the listed including -
[a-zA-Z0-9] Match One of the listed without -
$ End of string
Regex demo
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
How can i write a regexp, that will check if string starts and ends with digits and in between contains only digits and comas? Comas must also be separated from each other with at least one digit. For the conditions above i have following regexp: ^\d(,?\d)*$ but i have following additional condition: All comma separated integers, that are composed by sequences of digits, must be different from each other. What would be the regexp that allows only this kind of strings?
Thank you
First of all, your regex contains unquantified \d, and that matches only single digits. You need to add + after \d to match 1 or more digits.
To avoid having duplicate values, you may use
^(?!.*\b(\d+)\b.*\b\1\b)\d+(?:,\d+)*$
^^^^^^^^^^^^^^^^^^^^^^^
See the regex demo
The (?!.*\b(\d+)\b.*\b\1\b) is a negative lookahead that will fail the match if after any 0+ chars other than line break chars, there is a group of digits that appear later in the string (after another 0+ chars other than line break chars) again.
Details
^ - start of string
(?!.*\b(\d+)\b.*\b\1\b) - a negative lookahead that fails the match if identical values appear in the text
\d+ - 1+ digits
(?:,\d+)* - zero or more occurrences of
, - a comma
\d+ - 1+ digits
$ - end of string.
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.