1、校验手机号是否正确
public class Main {
public static void main(String args[]) {
String tel="15800001123";
//第一位1,第二位3或者5或者8,\\d{9}代表0-9出现了9次
String regex="1[358]\\d{9}";
boolean b=tel.matches(regex);
System.out.println(b);//true
}
}
2、隐藏手机号
public class Main {
//运行结果173****1112
public static void main(String args[]) {
String temp="17300001112";
//利用捕获组将手机号分为3组,在第二个字符串中调用这两个捕获组
temp=temp.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
System.out.println(temp);
}
}
3、去结巴
public class Main {
//运行结果:我要学编程
public static void main(String args[]) {
String temp="我我...我我我...我要要要..要要.学学学..学学编.编编..编编程程.程.程程";
temp=temp.replaceAll("\\.","");//去点
temp=temp.replaceAll("(.)\\1+","$1");//去叠词
System.out.println(temp);
}
}
4、邮箱验证
public class Main {
public static void main(String args[]) {
String mail="abc12@sina.com";
//\\w代表字母数字下换线,+代表一个或者多个,@固定字符,@后面一个或者多个字母数字,然后就是.和.后面的2~3个字母,例如.com
//然后就是.com重复1~3遍
String regex="\\w+@[a-zA-Z0-9]+(\\.[a-zA-Z]{2,3}){1,3}";
boolean b=mail.matches(regex);
System.out.println(b);
}
}
评论区