The string contains at least one digit

I'm trying to figure out if a string contains at least a number, or a lowercase or uppercase letter.

I wrote something like this:

int combinations = 0; string pass = "!!!AAabas1"; if (pass.matches("[0-9]")) { combinations = combinations + 10; } if (pass.matches("[az]")) { combinations =combinations + 26; } if (pass.matches("[AZ]")) { combinations =combinations + 26; } 

However, I don’t understand why I can’t get combinations up to 36. They just stay at 0. What am I doing wrong?

+4
source share
5 answers

You can use a template instead, I think the matches method searches for an entire string according to a regular expression.

Try the following code:

  int combinations = 0; String pass = "!!AAabas1"; if (Pattern.compile("[0-9]").matcher(pass).find()) { combinations = combinations + 10; } if (Pattern.compile("[az]").matcher(pass).find()) { combinations = combinations + 26; } if (Pattern.compile("[AZ]").matcher(pass).find()) { combinations = combinations + 26; } 
+10
source

Here is my attempt. Please note that this uses unicode categories for validation, so it is not Latin.

 import java.util.regex.Pattern; public class PasswordValidator { public static void main(String[] args) { final PasswordValidator passwordValidator = new PasswordValidator(); for (String password : new String[] { "abc", "abc123", "ABC123", "abc123ABC", "!!!AAabas1", "", "22" }) { System.out.printf("Password '%s' is %s%n", password, passwordValidator.isValidPassword(password) ? "ok" : "INVALID"); } } private static final Pattern LOWER_CASE = Pattern.compile("\\p{Lu}"); private static final Pattern UPPER_CASE = Pattern.compile("\\p{Ll}"); private static final Pattern DECIMAL_DIGIT = Pattern.compile("\\p{Nd}"); /** * Determine if a password is valid. * * <p> * A password is considered valid if it contains: * <ul> * <li>At least one lower-case letter</li> * <li>At least one upper-case letter</li> * <li>At least one digit</li> * </p> * * @param password * password to validate * @return True if the password is considered valid, otherwise false */ public boolean isValidPassword(final String password) { return containsDigit(password) && containsLowerCase(password) && containsUpperCase(password); } private boolean containsDigit(final String str) { return DECIMAL_DIGIT.matcher(str).find(); } private boolean containsUpperCase(final String str) { return UPPER_CASE.matcher(str).find(); } private boolean containsLowerCase(final String str) { return LOWER_CASE.matcher(str).find(); } } 

Here's the conclusion:

 Password 'abc' is INVALID Password 'abc123' is INVALID Password 'ABC123' is INVALID Password 'abc123ABC' is ok Password '!!!AAabas1' is ok Password '' is INVALID Password '22' is ok 
+4
source

The problem is that matches tries to match the entire input line.

Instead, try creating a Pattern , and then create a Matcher , and then use the find method.

The javadoc template should help a lot.

+2
source

When using a regex for this, obviously, you can work, the Guava CharMatcher class may be more appropriate for what you are trying to do:

 if (CharMatcher.inRange('0', '9').matchesAnyOf(pass)) combinations += 10; if (CharMatcher.inRange('a', 'z').matchesAnyOf(pass)) combinations += 26; if (CharMatcher.inRange('A', 'Z').matchesAnyOf(pass)) combinations += 26; 
+1
source
 String str_rno = "CooL8"; boolean Flag = false; String[] parts = str_rno.split(""); for (int i = 0; i < str_rno.length(); i++) { String part1 = parts[i + 1]; if (Character.isDigit(str_rno.charAt(i))) { System.out.println(" " + i + " " + part1 + " digit"); Flag = true; } else { System.out.println(" " + i + " " + part1 + " char "); } } if(Flag==true){ Toast.makeText(getApplicationContext(),"String contain 1 digit",Toast.LENGTH_SHORT).show(); } if(Flag==flase){ Toast.makeText(getApplicationContext(),"String not contain 1 digit",Toast.LENGTH_SHORT).show(); } 
+1
source

All Articles