二,public String replaceAll(String regex,String newStr) --- 按照正则的规则进行字符串的替换 String s1 = "张三123456789李四asdfjlkvajd王五";//要把字符串替换为 张三和李四和王五 //查找符合 数字 和 字母的正则 String regex = "\\w+";//(英文,数字,下划线)可以出现1次或多次 String result = s1...
check(str); }privatestaticvoidcheck(String str) {//匹配第一位是1-9,第二位及以后0-9(个数在4-10之间)String regex = "[1-9][0-9]{4,10}";/*//匹配单个字符是大小写的a-z String regex = "[a-zA-Z]"; //匹配数字,注意转义字符 String regex = "\\d"; //匹配非数字 String regex ...
{ Scanner sc = new Scanner(System.in); System.out.println("请输入你的QQ号码:"); String qq = sc.next(); System.out.println(checkQQ2(qq));}//使用正则表达式验证private static boolean checkQQ2(String qq){ String regex = "[1-9]\\d{4,14}";//正则表达式 return qq.matches(regex);...
此时校验月份的正则方法代码如下所示: // 校验两位的月份字符串public static void checkMonth() { String regex = "0[1-9]|1[0-2]"; for (int i=0; i<=13; i++) { String month = String.format("%02d", i); boolean check = month.matches(regex); System.out.println("month = "+month+...
public static void checkArea() { String regex = "(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|8[1-3])\\d{4}"; for (int i=0; i<=90; i++) { String area = String.format("%06d", i*10000); boolean check = area.matches(regex); ...
publicstaticvoidcheckArea() { String regex ="(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|8[1-3])\\d{4}"; for(inti=0; i<=90; i++) { String area = String.format("%06d", i*10000); booleancheck = area.matches(regex); ...
public Integer findTheWord(String stringToCheck, String regexString) throws IOException { int count = 0; Pattern regexp = Pattern.compile("\\b" + regexString + "\\b"); Matcher matcher = regexp.matcher(stringToCheck); while (matcher.find()) { count++; String matchString = matcher....
275publicstaticBoolean checkRegex(String regex, String str) { 276 277Pattern pattern = Pattern.compile(regex); 278 279Matcher matcher = pattern.matcher(str); 280 281returnmatcher.matches(); 282 283} 284 285/** 286 287* 验证正整数
voidcheckLookingAt(String regex,String content){Pattern p=Pattern.compile(regex);Matcher m=p.matcher(content);if(m.lookingAt()){System.out.println(content+"\tlookingAt: "+regex);}else{System.out.println(content+"\tnot lookingAt: "+regex);}}privatestaticvoidcheckFind(String regex,String ...
Stringstr = "12435423a2"; booleanflag = str.matches("[0-9]+"); System.out.println(flag?"输入正确":"只能输入数字"); } } 二、需求:校验QQ号,要求:必须是5~15位数字,0不能开头。 //没有正则表达式之前 publicstaticvoidcheckQQ(Stringqq) ...