public class Demo {
public static void main(String[] args) {
//匹配电话号码
String phoneNumber="0356-4839005";
boolean b = phoneNumber.matches("\\d{3,4}-\\d{7,8}");
if(b){
System.out.println("电话号码格式正确");
}else{
System.out.println("电话号码格式不正确");
}
//匹配手机号码
String phone="13100001111";
boolean ph = phone.matches("[1][3-9]\\d{9}");
//第一位是1,第二位是3-9之间的数,再加9个数字
if(ph){
System.out.println("手机号码格式正确");
}else{
System.out.println("手机号码格式不正确");
}
//匹配用户名,字母开头,数字字母下划线组合
String username="abc123";
username.matches("[a-zA-Z]+[\\w|_]*");
//+是一次或多次 w是字母加数字 *表示0或者多个
//匹配IP地址
String ip="20,10,20,123";
ip.matches("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}");
//匹配网址
String addr="tttp://www.baidu.com";
addr.matches("http://\\w+.\\w+.\\s*"); //w表示数字或字母 +是最后一次
//匹配年龄
String age="18";
age.matches("\\d{1,2}");
//匹配金额
String price="18.5";
price.matches("\\d+.\\d+.");
}
}
常用的正则表达式
最新推荐文章于 2025-12-19 14:09:09 发布
5631

被折叠的 条评论
为什么被折叠?



