String contactInfo = "12345678909";
/**
* 1 匹配手机号码
* Matcher类 matcher()方法
*/
private static final Pattern sqlPattern = Pattern.compile("^1[3|4|5|7|8|9][0-9]{9}$");
Matcher matcher = sqlPattern.matcher(contactInfo);
if (matcher.find()) {
return "匹配";
}
/**
* 2 匹配手机号码
* Pattern类 matches()方法
*/
if (Pattern.matches("^1[3|4|5|7|8|9][0-9]{9}$", contactInfo)) {
return PHONE;
}
/**
* 3 匹配手机号码
* String类 matches()方法
*/
boolean b = contactInfo.matches("^1[3|4|5|7|8|9][0-9]{9}$");
// 匹配纯数字
String reg = "^(\\d+)$";
// 匹配中文
String reg = "^[\u4E00-\u9FA5]$";
// sql防注入
String reg = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|"
+ "(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";
// 匹配邮箱
String reg = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
// 匹配手机号码
String reg = "^1[3|4|5|7|8|9][0-9]{9}$";
Java常用代码的实现——正则匹配
最新推荐文章于 2024-07-27 13:31:57 发布