String containing alphanumeric in Java


Times we need to ensure the particular combinations of alphanumeric characters in String. For example, an employee Id, Student roll number or any department can be in the form of alphanumeric String. So here we are going to see a code sample which would help to determine if a string contains alphanumeric combination or not. We can also find the particular number of alphabets and numeric it contains. We will also look at the different ways to find alphabets and digits in String as well as count of them.

We have already covered the part where we saw the sample code to check if String contains only numeric digits or if String contains alphabets only . To do so, Java provides regular expressions using java.util.regex.Pattern and java.util.regex.Matchter class.

Sample code to Check if String contains alphanumeric in Java

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GoyalsBitStringTest {

    // This method would check for the alphanumeric strings
    // There can be any number of Alphabets followed by any number of digits in the String
    public static boolean isAlphaNumericWay1(String id){
        Pattern p = Pattern.compile("^[a-zA-Z]+[0-9]+");
        Matcher m = p.matcher(id);
        boolean b = m.matches();
        return b;

    // This method would check for the alphanumeric strings
    // There can be only 2 Alphabets followed by 5 digits in the String
    public static boolean isAlphaNumericWay2(String id){
        Pattern p = Pattern.compile("^[a-zA-Z][a-zA-Z]\\d{5}");
        Matcher m = p.matcher(id);
        boolean b = m.matches();
        return b;
    public static void main(String[] args) {
        System.out.println("AAAAAAAAAA345435366 conatins n chars and  n digits : " + isAlphaNumericWay1("AAAAAAAAAA345435366"));
        System.out.println("AB12345 conatins 2 chars and  5 digits : " + isAlphaNumericWay2("AB12345"));



And here is the output it provides :

AAAAAAAAAA345435366 contains n chars and  n digits : true
AB12345 contains 2 chars and  5 digits : true


What’s your approach to find the alphanumeric combinations ? Share in comments below.

Previous articleCheck if String contains Alpha Chars in Java
Next articleInstallation of Java – Step by Step Guide
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.


  1. Thanks for the marvelous posting! I definitely enjoyed reading it,
    you happen to be a great author. I will make certain to bookmark your blog
    and will come back from now on. I want to encourage one to continue your
    great work, have a nice afternoon!