Java String match six digits
import java.util.regex.Pattern; public class Main{ public static void main(String[] argv) throws Exception{ String str = "123"; System.out.println(isValidCode(str)); //from w ww.ja va2 s . com str = "123123"; System.out.println(isValidCode(str)); } public static boolean isValidCode(String str) { if (str==null || str.length() == 0) { return false; } String regx = "^[0-9]{6}$"; Pattern pattern = Pattern.compile(regx); return pattern.matcher(str).matches(); } }