Regex for at least 3 (numbers and special characters) in Android

Long Dao

I have a password rule that needs to satisfy those condition below:

At least 2 out of the following: - At least 1 lowercase character - At least 1 uppercase character - At least 2 (numbers AND special characters)

I build my regex like this below:

String oneLowercaseCharacter = ".*[a-z].*";
String oneUppercaseCharacter = ".*[A-Z].*";
String oneNumber = ".*\\d.*";
String oneSpecialCharacter = ".*[^\\`\\~\\<\\,\\>\\\"\\'\\}\\{\\]\\[\\|\\)\\(\\;\\&\\*\\$\\%\\#\\@\\!\\:\\.\\/\\?\\\\\\+\\=\\-\\_\\ ].*";
String threeNumbersAndCharacters = ".*[0-9\\^\\`\\~\\<\\,\\>\\\"\\'\\}\\{\\]\\[\\|\\)\\(\\;\\&\\*\\$\\%\\#\\@\\!\\:\\.\\/\\?\\\\\\+\\=\\-\\_\\ ].*[0-9\\^\\`\\~\\<\\,\\>\\\"\\'\\}\\{\\]\\[\\|\\)\\(\\;\\&\\*\\$\\%\\#\\@\\!\\:\\.\\/\\?\\\\\\+\\=\\-\\_\\ ].*[0-9\\^\\`\\~\\<\\,\\>\\\"\\'\\}\\{\\]\\[\\|\\)\\(\\;\\&\\*\\$\\%\\#\\@\\!\\:\\.\\/\\?\\\\\\+\\=\\-\\_\\ ].*";

And then I build the function like this below:

if ((Pattern.compile(oneLowercaseCharacter).matcher(s).find() && Pattern.compile(oneUppercaseCharacter).matcher(s).find())
                            || (Pattern.compile(oneLowercaseCharacter).matcher(s).find()
                                && Pattern.compile(oneSpecialCharacter).matcher(s).find()
                                && Pattern.compile(oneNumber).matcher(s).find()
                                && Pattern.compile(threeNumbersAndCharacters).matcher(s).find())
                            || (Pattern.compile(oneUppercaseCharacter).matcher(s).find()
                                && Pattern.compile(oneSpecialCharacter).matcher(s).find()
                                && Pattern.compile(oneNumber).matcher(s).find()
                                && Pattern.compile(threeNumbersAndCharacters).matcher(s).find())) {
//Do my stuff here
}

However, it does not work as expected. Not really sure why but if I test with different passwords, results show like this:

qwerty123 true (not expected)

qwerty!@# false

qwerty12. true

Qwerty123 true

Qwerty12. true

Anyone has any idea where I missed?

Note: I search around stackoverflow already and look elsewhere already so that I came up with the above code, however could not go further.

ROMANIA_engineer

The problem is in this line:

String oneSpecialCharacter = ".*[^\\`\\~\\<\\,\\>\\\"\\'\\}\\{\\]\\[\\|\\)\\(\\;\\&\\*\\$\\%\\#\\@\\!\\:\\.\\/\\?\\\\\\+\\=\\-\\_\\ ].*";

The character ^ has a special meaning ("not") when it is used in the first position inside [].

This is why you need to escape it.

String oneSpecialCharacter = ".*[\\^\\`\\~\\<\\,\\>\\\"\\'\\}\\{\\]\\[\\|\\)\\(\\;\\&\\*\\$\\%\\#\\@\\!\\:\\.\\/\\?\\\\\\+\\=\\-\\_\\ ].*";

Now your result should looks like this:

qwerty123 -> false
qwerty!@# -> false 
qwerty12. -> true
Qwerty123 -> true
Qwerty12. -> true

Other examples that emphasize the meaning of ^:

// the first character cannot be a
System.out.println(Pattern.compile("[^a]bc").matcher("abc").find());        // false

// the first character cannot be x, y or z, but it can be a
System.out.println(Pattern.compile("[^xyz]bc").matcher("abc").find());      // true

// the first character can be ^ or a
System.out.println(Pattern.compile("[\\^a]bc").matcher("abc").find());      // true

// the first character can be ^, x, y or z, but not a
System.out.println(Pattern.compile("[\\^xyz]bc").matcher("abc").find());    // false

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Regex with numbers and special characters but no letters

Regex 2 Numbers OR Special Characters

Regex containing at-least 3 of the following [0-9], [a-z], [A-Z] or special characters (except $)

Regex remove all special characters except numbers?

Regex for Numbers, some special characters and NULL

Regex to validate at least two character classes (letters, number , special characters)

Regex for at least 10 alphanumeric characters that contain at least 3 digits

RegEx for 4 characters and 7 numbers. No special characters

Regex matching string containing at least x characters and x numbers

regex to remove special characters in mule3

Regex for strings containing at least 4 unique printable ASCII characters excluding few special characters

Regex javascript for Minimum 8 characters,at least one number and one special character, maximum 32 characters

Regex not allow more than 3 same characters and at least 3 words

using regex to match a string including with numbers and special characters

Manipulate Regex to ignore the space and disallow numbers and special characters

Suggest a regex that matches a string with letters and (numbers or special characters)

Python regex: removing all special characters and numbers NOT attached to words

regex - only allow English(lower or upper), numbers, special characters

Regex to strip all numbers and special characters but space and letters

Python regex to extract positive and negative numbers between two special characters

javascript regex to allow numbers and special characters but not zeroes only

Regex validate string contains letters and numbers regardless of special characters

Regex: match at least 3 characters in a string in specific order

Most efficient regex for checking if a string contains at least 3 alphanumeric characters

Get numbers and characters after 3 expression with regex

Using regex to verify an input has at least 2 letters, with special and normal characters allowed

Java regex require at least one letter and one digit. Also allow any special characters

Regex to match pattern of at least 7 digits and any amount of special characters in any order

Regex that contains at least 1 uppercase, lowercase, numbers, 8 characteres, special character and DOES NOT CONTAIN WHITESPACE