2

I am trying to check if string (user input) is strictly alphanumeric or not.

I tried following check in java

if (str.matches("^[\\p{Alnum}]*$")){
   System.out.println("String is Alphanumeric");
}

This paases for all following input

rfvtgbyhnujm
235675434567
3edc4rfv5tgb
cde3vfr4bgt5

I just want it to pass for 3edc4rfv5tgb & cde3vfr4bgt5.

almost all forums suggested this solution, but it doesn't work for me. Don't know what I am missing here.

I just want it to pass for pure alpha-numeric strings, no special characters.

roy
  • 6,344
  • 24
  • 92
  • 174
  • Just test each character in the string - check out ascii table and you'll know what to do. – phg1024 Mar 28 '17 at 01:47
  • Refer this - http://stackoverflow.com/questions/336210/regular-expression-for-alphanumeric-and-underscores – vlaxmi Mar 28 '17 at 01:49
  • maybe lets us know what you don't want included – Scary Wombat Mar 28 '17 at 02:13
  • @roy you said you want only numbers and letters to be accepted why don't you try looping if regex doesn't work for your problem using the String and Character class methods – 0xDEADBEEF Mar 28 '17 at 02:52
  • `rfvtgbyhnujm` and `235675434567` IS alphanumeric. Do you mean you want it to contains both alpha and numeric characters? – Adrian Shum Mar 28 '17 at 03:15

4 Answers4

4

You may match with the following logic:

  1. If the text starts with a sequence of alphabetic characters, there must be a digit afterwards, \p{Alpha}++\p{Digit}, or
  2. If the text starts with a sequence of digits, there must be an alphabetic character afterwards, \p{Digit}++\p{Alpha}

After either of these alternatives, arbitrary alphanumeric trailing characters may follow:

^(?:\p{Alpha}++\p{Digit}|\p{Digit}++\p{Alpha})\p{Alnum}*$

Test:

String[] samples={ "rfvtgbyhnujm", "235675434567", "3edc4rfv5tgb", "cde3vfr4bgt5" };
for(String str: samples) {
    if(str.matches("^(?:\\p{Alpha}++\\p{Digit}|\\p{Digit}++\\p{Alpha})\\p{Alnum}*$")) {
        System.out.println(str+" is Alphanumeric");
    }
}
3edc4rfv5tgb is Alphanumeric
cde3vfr4bgt5 is Alphanumeric
Holger
  • 285,553
  • 42
  • 434
  • 765
2

Not sure what's your "pure alpha-numeric" means. If you means the string should contains both "alpha" and "numeric" characters, you can do the trick by making use of positive lookahead:

^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]+$

Explanation

^                                        start of line
 (?=          )                          positive look ahead
    .*[a-zA-Z]                           any chars followed by alphabets
               (?=       )               another positive look ahead
                  .*[0-9]                any chars followed by digits
                          [a-zA-Z0-9]+   alphanumeric characters
                                      $  till end of line

Test it out in https://regex101.com/r/YNmBx4/1

One advantage of using this approach is, it is easy to add more criteria to the regex, for example, if you want at least 1 uppercase character in the string, simply add one positive lookahead (?=.*[A-Z])

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
1

As I understand, you will want at least one letter and at least one digit to accept your string as alphanumeric (this is not the usual definition). To take into account that either the letter or the digit may come first, I suggest the following.

Edit: Inspired by Holger’s answer I have introduced the possessive quantifier, ++. Like the (usual) greedy quantifier it matches as many instances as possible, but unlike the greedy quantifier, if a match of the full pattern is not found, it does not go back and try with fewer instances. It seems this makes sense here (and gives better performance).

private static final Pattern alphanumPattern 
        = Pattern.compile("(\\p{Alpha}++\\p{Digit}++\\p{Alnum}*)|(\\p{Digit}++\\p{Alpha}++\\p{Alnum}*)");

private static boolean isAplhanumeric(String str) {
    return alphanumPattern.matcher(str).matches();
}

This passes for 3edc4rfv5tgb and cde3vfr4bgt5, but rejects rfvtgbyhnujm and 235675434567.

Community
  • 1
  • 1
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

To match a string that contains only those characters (or an empty string), try below regex -

"^[a-zA-Z0-9_]*$"

This works for .NET regular expressions, and probably a lot of other languages as well.

Breaking it down:

^ : start of string
[ : beginning of character group
a-z : any lowercase letter
A-Z : any uppercase letter
0-9 : any digit
_ : underscore
] : end of character group
* : zero or more of the given characters
$ : end of string

If you don't want to allow empty strings, use + instead of *.

Pure-Alpha-Numberic solution is :

String input = "3edc4rfv5tgb";
System.out.println("matched :"+Pattern.compile("^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]+$").matcher(input));
vlaxmi
  • 468
  • 4
  • 18