Main Tutorials

Java regex check alphanumeric string

The Alphanumericals are a combination of alphabetical [a-zA-Z] and numerical [0-9] characters, 62 characters.

We can use below regex to match alphanumeric characters:


  ^[a-zA-Z0-9]+$

Regex explanation


  ^       # start string
  [a-z]   # lowercase letters from a to z
  [A-Z]   # uppercase letters from A to Z
  [0-9]   # digits from 0 to 9
  +       # one or more characters
  $       # end string

1. Java Regex Alphanumeric

Below is a Java regex to match alphanumeric characters.

StringUtils.java

package com.mkyong.regex.string;

public class StringUtils {

    private static final String ALPHANUMERIC_PATTERN = "^[a-zA-Z0-9]+$";

    public static boolean isAlphanumeric(final String input) {
        return input.matches(ALPHANUMERIC_PATTERN);
    }

}

Below is a simple JUnit 5 tests to validate the alphanumeric characters.

StringUtilsTest.java

package com.mkyong.regex.string;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class StringUtilsTest {

    @ParameterizedTest(name = "#{index} - Run test with input = {0}")
    @MethodSource("validAlphanumericProvider")
    void test_alphanumeric_regex_valid(String input) {
        assertTrue(StringUtils.isAlphanumeric(input));
    }

    static Stream<String> validAlphanumericProvider() {
        return Stream.of(
                "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
                "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
    }

    @ParameterizedTest(name = "#{index} - Run test with input = {0}")
    @MethodSource("invalidAlphanumericProvider")
    void test_alphanumeric_regex_invalid(String input) {
        assertFalse(StringUtils.isAlphanumeric(input));
    }

    static Stream<String> invalidAlphanumericProvider() {
        return Stream.of(
                "_",                        // underscore
                "@",                        // symbols
                " ",                        // space
                "");                        // empty
    }

}

2. Regex Alphanumeric and Underscore

The regex \w is equivalent to [A-Za-z0-9_], matches alphanumeric characters and underscore.

StringUnderscore.java

package com.mkyong.regex.string;

public class StringUnderscore {

    public static void main(String[] args) {

        String str = "Hello_World_Java";

        // or ^[\\w]+$   
        if (str.matches("^[a-zA-Z0-9_]+$")) {
            System.out.println("Yes, true.");
        } else {
            System.out.println("failed!");
        }
    }
}

Output

Terminal

  Yes, true.

3. Regex Alphanumeric and Space

The regex \\s matches whitespace characters.

StringSpace.java

package com.mkyong.regex.string;

public class StringSpace {

    public static void main(String[] args) {

        String str = "Hello World Java";
        if (str.matches("^[a-zA-Z0-9\\s]+$")) {
            System.out.println("Yes, true.");
        } else {
            System.out.println("failed!");
        }
    }
}

Output

Terminal

  Yes, true.

Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-regex/string

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments