Regular expression for a string that must contain minimum 14 characters, where at minimum 2 are numbers, and at minimum 6 are letters
Easy! First lets look at a commented version in PHP:
$re = '/# Match 14+ char password with min 2 digits and 6 letters.
^ # Anchor to start of string.
(?=(?:.*?[A-Za-z]){6}) # minimum of 6 letters.
(?=(?:.*?[0-9]){2}) # minimum of 2 numbers.
[A-Za-z0-9#,.\-_]{14,} # Match minimum of 14 characters.
$ # Anchor to end of string.
/x';
Here is the JavaScript version:
var re = /^(?=(?:.*?[A-Za-z]){6})(?=(?:.*?[0-9]){2})[A-Za-z0-9#,.\-_]{14,}$/;
Addendum 2012-11-30
I noticed that this answer recently got an upvote. This uses a more outdated expression so I figured it was time to update it with a better one.
=== A more efficient expression ===
By getting rid of the "dot-star" altogether and greedily applying a more precise expression, (a negated char class), an even more efficient solution results:
$re = '/# Match 14+ char password with min 2 digits and 6 letters.
^ # Anchor to start of string.
(?=(?:[^A-Za-z]*[A-Za-z]){6}) # minimum of 6 letters.
(?=(?:[^0-9]*[0-9]){2}) # minimum of 2 numbers.
[A-Za-z0-9#,.\-_]{14,} # Match minimum of 14 characters.
$ # Anchor to end of string.
/x';
Here is the new JavaScript version:
var re = /^(?=(?:[^A-Za-z]*[A-Za-z]){6})(?=(?:[^0-9]*[0-9]){2})[A-Za-z0-9#,.\-_]{14,}$/;
- Edit 1: Added
#,.-_
to list of valid chars. - Edit 2: Changed the greedy to lazy star.
- Edit 2012-11-30: Added alternate version with the "lazy-dot-star" replaced with a more efficient greedy application of a more precise expression.
Regex to determine if string contains at least 2 numbers
Try this:
- .* matches everything (from 0 to n times)
- [0-9] is number from 0-9
Pattern pattern = Pattern.compile("[0-9].*[0-9]");Matcher matcher = pattern.matcher(string);if (matcher.find()) { return true;}
Minimum 6 characters regex
This match 6 or more any chars but newline:
/^.{6,}$/
Password validator javascript regular expression
You can add another lookahead in your regex:
/^(?=.*[0-9])(?=(?:[^A-Z]*[A-Z]){2})(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,12}$/;
Need help to create one regex for password validation
For the rules you mentioned:
/^(?=.*[0-9])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])/;
This should suffice.
[^a-zA-Z0-9]
matches anything that is not an alphabet and not a number, i.e. a special character.
I removed the case insensitive flag because you specifically mentioned there should be 1 uppercase letter.
Minimum special characters, Uppercase and numbers
Without understanding the question I'm pretty sure you are looking for the positive or negative lookaround which is in most regex dialects.
For example:
( # Start of group
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[A-Z]) # must contains one uppercase characters
(?=.*[@#$%]) # must contains one special symbols in the list "@#$%"
. # match anything with previous condition checking
{6,20} # length at least 6 characters and maximum of 20
) # End of group
source: https://www.mkyong.com/regular-expressions/how-to-validate-password-with-regular-expression/
Password regex that requires “at least two of” certain characters
^(?!.*[^A-Za-z0-9#?!@$%^&*-]$)(?![a-z]*$)(?![A-Z]*$)(?![0-9]*$)(?![#?!@$%^&*-]*$).{8,64}$
The string should not contain any symbol outside the 4 groups of characters
^(?!.*[^A-Za-z0-9#?!@$%^&*-]$)
The string should not consist only of lower letters
(?![a-z]*$)
The string should not consist only of upper letters
(?![A-Z]*$)
The string should not consist only of digits
(?![0-9]*$)
The string should not consist only of special characters
(?![#?!@$%^&*-]*$)
The string should consist of 8 to 64 characters
.{8,64}$
UPDATED 2020-09-07
If the string should contain symbols of at list 3 groups of 4
^(?!.*[^A-Za-z0-9#?!@$%^&*-]$)((?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])|(?=.*[a-z])(?=.*[A-Z])(?=.*[#?!@$%^&*-])|(?=.*[a-z])(?=.*[0-9])(?=.*[#?!@$%^&*-])|(?=.*[A-Z])(?=.*[0-9])(?=.*[#?!@$%^&*-])).{8,64}$
The string should not contain any symbol outside the 4 groups of characters
^(?!.*[^A-Za-z0-9#?!@$%^&*-]$)
Then 4 variants of 3 groups of 4 that the symbols should be member of:
(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])
or
(?=.*[a-z])(?=.*[A-Z])(?=.*[#?!@$%^&*-])
or
(?=.*[a-z])(?=.*[0-9])(?=.*[#?!@$%^&*-])
or
(?=.*[A-Z])(?=.*[0-9])(?=.*[#?!@$%^&*-])
and finally the string should consist of 8 to 64 characters
.{8,64}$
Regex pattern to match at least 1 number and 1 character in a string
Why not first apply the whole test, and then add individual tests for characters and numbers? Anyway, if you want to do it all in one regexp, use positive lookahead:
/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/
Related Topics
Windows Cmd.Exe "The System Cannot Find the Path Specified."
Weak Typing in PHP: Why Use Isset at All
How to Access a Variable Across Two Files
Inserting Utf-8 Encoded String into Utf-8 Encoded MySQL Table Fails with "Incorrect String Value"
Execute Python Script from PHP
A PHP/Pthreads Thread Class Can't Use Array
Detecting File Upload Size on the Client Side
Adding Multiple Tabs to Woocommerce Single Product Pages
Turn Plain Text Urls into Active Links Using PHP
How to Convert Array to a String Using Methods Other Than JSON
Is There an Equivalent in C++ of PHP's Explode() Function
Require_Once :Failed to Open Stream: No Such File or Directory
Codeigniter - Correct Way to Link to Another Page in a View