正则表达式
标签: java
正则表达式的类:java.util.regex.Pattern, 即Pattern类
例如:
String phone = "19953772902";
Pattern pnum = Pattern.compile("1[2-9]\\d{9}");//这个里面写模式
if(pnum.matcher(phone).find()){
System.out.println("格式符合要求");
}else{
System.out.println("格式不符合要求");
}
//如果仅仅是为了检验格式,可以直接使用静态方法
if(Pattern.matches("1[2-9]\\d{9}",phone)){
System.out.println("格式符合要求");
}else{
System.out.println("格式不符合要求");
}
正则表达式基本模式
例如邮箱的格式表达:
Pattern email = Pattern.compile("\\w+@\\w+([.]\\w{2,4}){1,2}");
String myemail = 451825688@qq.com;
if(email.matcher(myemail).find()){
System.out.println("格式符合要求");
}else{
System.out.println("格式不符合要求");
}