Regex for checking if a string is strictly alphanumeric

JavaRegexAlphanumeric

Java Problem Overview


How can I check if a string contains only numbers and alphabets ie. is alphanumeric?

Java Solutions


Solution 1 - Java

Considering you want to check for ASCII Alphanumeric characters, Try this: "^[a-zA-Z0-9]*$". Use this RegEx in String.matches(Regex), it will return true if the string is alphanumeric, else it will return false.

public boolean isAlphaNumeric(String s){
	String pattern= "^[a-zA-Z0-9]*$";
	return s.matches(pattern);
}

If it will help, read this for more details about regex: http://www.vogella.com/articles/JavaRegularExpressions/article.html

Solution 2 - Java

In order to be unicode compatible:

^[\pL\pN]+$

where

\pL stands for any letter
\pN stands for any number

Solution 3 - Java

It's 2016 or later and things have progressed. This matches Unicode alphanumeric strings:

^[\\p{IsAlphabetic}\\p{IsDigit}]+$

See the reference (section "Classes for Unicode scripts, blocks, categories and binary properties"). There's also this answer that I found helpful.

Solution 4 - Java

See the documentation of Pattern.

Assuming US-ASCII alphabet (a-z, A-Z), you could use \p{Alnum}.

A regex to check that a line contains only such characters is "^[\\p{Alnum}]*$".

That also matches empty string. To exclude empty string: "^[\\p{Alnum}]+$".

Solution 5 - Java

Use character classes:

^[[:alnum:]]*$

Solution 6 - Java

Pattern pattern = Pattern.compile("^[a-zA-Z0-9]*$");
Matcher matcher = pattern.matcher("Teststring123");
if(matcher.matches()) {
     // yay! alphanumeric!
}

Solution 7 - Java

try this [0-9a-zA-Z]+ for only alpha and num with one char at-least..

may need modification so test on it

http://www.regexplanet.com/advanced/java/index.html

Pattern pattern = Pattern.compile("^[0-9a-zA-Z]+$");
Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.matches()) {
 
}

Solution 8 - Java

To consider all Unicode letters and digits, Character.isLetterOrDigit can be used. In Java 8, this can be combined with String#codePoints and IntStream#allMatch.

boolean alphanumeric = str.codePoints().allMatch(Character::isLetterOrDigit);

Solution 9 - Java

To include [a-zA-Z0-9_], you can use \w.

So myString.matches("\\w*"). (.matches must match the entire string so ^\\w*$ is not needed. .find can match a substring)

https://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

Solution 10 - Java

100% alphanumeric RegEx (it contains only alphanumeric, not even integers & characters, only alphanumeric)

For example:

> special char (not allowed)
> 123 (not allowed)
>asdf (not allowed)
> 1235asdf (allowed)


String name="^[^<a-zA-Z>]\\d*[a-zA-Z][a-zA-Z\\d]*$";

Solution 11 - Java

If you want to include foreign language letters as well, you can try:

String string = "hippopotamus";
if (string.matches("^[\\p{L}0-9']+$")){
    string is alphanumeric do something here...
}

Or if you wanted to allow a specific special character, but not any others. For example for # or space, you can try:

String string = "#somehashtag";
if(string.matches("^[\\p{L}0-9'#]+$")){
    string is alphanumeric plus #, do something here...
}

Solution 12 - Java

To check if a String is alphanumeric, you can use a method that goes through every character in the string and checks if it is alphanumeric.

    public static boolean isAlphaNumeric(String s){
            for(int i = 0; i < s.length(); i++){
                    char c = s.charAt(i);
                    if(!Character.isDigit(c) && !Character.isLetter(c))
                            return false;
            }
            return true;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionO__OView Question on Stackoverflow
Solution 1 - JavaRaghavView Answer on Stackoverflow
Solution 2 - JavaTotoView Answer on Stackoverflow
Solution 3 - JavaJohannes JanderView Answer on Stackoverflow
Solution 4 - JavasudocodeView Answer on Stackoverflow
Solution 5 - JavaIgor ChubinView Answer on Stackoverflow
Solution 6 - JavaSeth MalakiView Answer on Stackoverflow
Solution 7 - JavaDheeresh SinghView Answer on Stackoverflow
Solution 8 - JavaUnmitigatedView Answer on Stackoverflow
Solution 9 - JavaCurtis YallopView Answer on Stackoverflow
Solution 10 - JavaAmitView Answer on Stackoverflow
Solution 11 - JavaBrianG7View Answer on Stackoverflow
Solution 12 - JavaJustin CairnsView Answer on Stackoverflow