import java.util.regex.Matcher; import java.util.regex.Pattern; public class ZhengZe { public static void main(String[] args) { //6位数字 //1.指定正则表达式 //6位数字 Pattern p1= Pattern.compile("\d{6}"); //11位 以1开头 Pattern p2= Pattern.compile("1\d{10}"); //英文字母和数字组成 长度为4-10 Pattern p3= Pattern.compile("[a-zA-Z0-9]{4,10}"); //以英文字母开头和数字组成开头,长度为4-16 Pattern p4= Pattern.compile("[a-zA-Z][a-zA-Z0-9]{3,15}"); //2.设置要验证的字符串 Matcher m1 = p1.matcher("212121"); Matcher m2 = p2.matcher("11548741547"); Matcher m3 = p3.matcher("das4"); Matcher m4 = p4.matcher("dasdad4544"); //3获得结果 boolean rs1 = m1.matches(); boolean rs2 = m2.matches(); boolean rs3 = m3.matches(); boolean rs4 = m4.matches(); System.out.println(rs1); System.out.println(rs2); System.out.println(rs3); System.out.println(rs4); } }