/**
* 判断字符串满足指定格式 true 合法
*/
public static boolean checkValidity (String str, String regex) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if (m.matches()) {
return true;
} else {
return false;
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 使用正则表达式验证输入格式
* @author
*
*/
public class RegexValidateUtil {
public static void main(String[] args) {
System.out.println(checkEmail("liu927@qw.df"));
System.out.println(checkMobileNumber("18911729111"));
}
/**
* 验证手机号码是否合法
* @param
* @return
*/
public static boolean checkMobileNumber(String mobileNumber){
boolean flag = false;
try{
Pattern regex = Pattern.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
Matcher matcher = regex.matcher(mobileNumber);
flag = matcher.matches();
}catch(Exception e){
flag = false;
}
return flag;
}
/**
* 验证邮箱是否合法
* @param email
* @return
*/
public static boolean checkEmail(String email){
boolean flag = false;
try{
String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(email);
flag = matcher.matches();
}catch(Exception e){
flag = false;
}
return flag;
}
java字符串格式验证
最新推荐文章于 2024-06-29 03:22:14 发布