Java Regular Expression 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 www.j av a 2 s . c o m 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(); } }