2

I am trying to check whether a password is alphanumeric or not using regex but I am not getting the result I expect. What is the problem with the below code?

boolean passwordOnlyAlphaNumericCheck = false;
Pattern patternAlphaNumericCheck = Pattern.compile("^[a-zA-Z0-9]$");
Matcher matcherAlphaNumericCheck = patternAlphaNumericCheck.matcher(login.getPassword());
if(matcherAlphaNumericCheck.find())
  passwordOnlyAlphaNumericCheck = true;

Thanks for help

3
  • 3
    "^[a-zA-Z0-9]+$" or "^[a-zA-Z0-9]*" - you forgot the quantifier. And you can remove the ^ and $ and use .matches(). Commented May 6, 2016 at 10:33
  • @WiktorStribiżew it works when i use login.getPassword().matches("[0-9a-zA-Z]*"); but what is the difference between login.getPassword().matches("[0-9a-zA-Z]*"); and login.getPassword().matches("[0-9a-zA-Z]"); Commented May 6, 2016 at 10:39
  • See more explanations. Commented May 6, 2016 at 11:28

5 Answers 5

7

You need to add a quantifier that suits your requirements: * - 0 or more occurrences or + - 1 or more occurrences. You can also omit the ^ and $ and use String.matches():

boolean passwordOnlyAlphaNumericCheck = false;
if(login.getPassword().matches("[a-zA-Z0-9]*"))
  passwordOnlyAlphaNumericCheck = true;

To match all Unicode letters, use \p{L} class (and perhaps, \p{M} to match diacritics): "[\\p{L}\\p{M}0-9]+".

what is the difference between login.getPassword().matches("[0-9a-zA-Z]*"); and login.getPassword().matches("[0-9a-zA-Z]");?

The .matches("[0-9a-zA-Z]") will only return true if the whole string contains just 1 digit or letter. The * in [0-9a-zA-Z]* will allow an empty string, or a string having zero or more letters/digits.

Sign up to request clarification or add additional context in comments.

Comments

3

use this regex expression Take a Demo Here For Regex

"^[a-zA-Z0-9]*$ " Or "^[a-zA-Z0-9]+$"

instead of

"^[a-zA-Z0-9]$ // * or + should be added at last 

So, This might work to you

Pattern patternAlphaNumericCheck = Pattern.compile("^[a-zA-Z0-9]*$");

OR

Pattern patternAlphaNumericCheck = Pattern.compile("^[a-zA-Z0-9]+$");

Comments

2

Your pattern will only match strings with one alphanumeric character, since you have no quantifier and match explicitly from start (^) to end ($).

Append + for 1+ matches, or * for 0+ matches to your character class.

You can also use the script: \\p{Alnum} instead of a tedious character class.

For instance:

  • ^\\p{Alnum}{8}$ will match a String made of 8 alphanumeric characters
  • ^\\p{Alnum}{8,}$ will match a String made of 8 or more alphanumeric characters
  • ^\\p{Alnum}{8,10}$ will match a String made of 8 to 10 alphanumeric characters
  • ^\\p{Alnum}+$ will match a String made of 1 or more alnums
  • ^\\p{Alnum}*$ will match an empty String or a String made of any number of alnums

Comments

2

This function will help ensure that you have just alphanumeric as wanted.

public static boolean isAlphanumeric(String value){
    if (isValueEmpty(value)) {
        return false;
    }

    return value.matches("^[a-zA-Z_0-9]*$");
}

public static boolean isValueEmpty(String param){
    return param == null || param.isEmpty();
}

Comments

1

Try this regexp:

private final String nonAlphanumericRegex = ".*\\W.*";
if (Pattern.matches(nonAlphanumericRegex, YOUR_STRING))
    throw IllegalArgumentException()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.