Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String contains at least one digit

Tags:

java

regex

I am trying to see whether a string contains at least a digit or a lowercase or an uppercase.

I have written something like this:

      int combinations = 0;
      string pass = "!!!AAabas1";

      if (pass.matches("[0-9]")) {
          combinations = combinations + 10;
      }

      if (pass.matches("[a-z]")) {
          combinations =combinations + 26;
      }

      if (pass.matches("[A-Z]")) {
          combinations =combinations + 26;
      }

However I don't understand why I cannot get combinations to go to 36. They just remain at 0. What am I doing wrong?

like image 995
idipous Avatar asked Jun 21 '11 01:06

idipous


People also ask

How do you know if a string has at least one digit?

To check if a string contains at least one number using regex, you can use the \d regular expression character class in JavaScript. The \d character class is the simplest way to match numbers.

How do you check if a string contains at least one digit in Python?

To check if a string contains a number in Python:Use the str. isdigit() method to check if each char is a digit. Pass the result to the any() function. The any function will return True if the string contains a number.

How do you check if a string contains at least one character in JS?

To check if a string contains any letters, use the test() method with the following regular expression /[a-zA-Z]/ . The test method will return true if the string contains at least one letter and false otherwise.


2 Answers

You could use Pattern instead, I think "matches" method looks for the whole string to match the regular expression.

Try the next code:

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

    if (Pattern.compile("[a-z]").matcher(pass).find()) {
        combinations = combinations + 26;
    }

    if (Pattern.compile("[A-Z]").matcher(pass).find()) {
        combinations = combinations + 26;
    }
like image 123
Hrzio Avatar answered Oct 21 '22 03:10

Hrzio


Here's my attempt. Note, this uses unicode categories for validation so is non-latin language friendly.

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 output:

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
like image 24
hoipolloi Avatar answered Oct 21 '22 03:10

hoipolloi