Java Regular Expression match digits and letters only

Description

Java Regular Expression match digits and letters only


import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "asdf123HLJ";
        System.out.println(isLetterAndDigits(str));
    }/*from  w w w.  j a v  a  2s.c o m*/

    public static boolean isLetterAndDigits(String str) {

        String regx = "^[0-9a-zA-Z]+$";

        Pattern pattern = Pattern.compile(regx);
        return pattern.matcher(str).matches();
    }
}

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "";
        System.out.println(isNumberLetter(str));
    }/*  w  w w .  j  a  v a  2 s .  c o  m*/

    public static Boolean isNumberLetter(String str) {
        Boolean isNoLetter = false;
        String expr = "^[A-Za-z0-9]+$";
        if (str.matches(expr)) {
            isNoLetter = true;
        }
        return isNoLetter;
    }
}



PreviousNext

Related