您的位置:首页 > 其它

常用的正则表达式

2016-06-25 13:50 225 查看
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+.");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: