正则表达式验证格式 手机、邮箱、字符串

前端之家收集整理的这篇文章主要介绍了正则表达式验证格式 手机、邮箱、字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

手机号码验证

  1. public static boolean isMobileNO(String mobiles) {
  2. try {
  3. Pattern p = Pattern
  4. .compile("(13[0-9]|14[57]|15[012356789]|18[02356789])\\d{8}");
  5. Matcher m = p.matcher(mobiles);
  6. return m.matches();
  7. } catch (Exception e) {
  8. return false;
  9. }
  10. }

验证邮箱地址是否正确

  1. public static boolean checkEmail(String email) {
  2. try {
  3. String check = "([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}";
  4. Pattern regex = Pattern.compile(check);
  5. Matcher matcher = regex.matcher(email);
  6. return matcher.matches();
  7. } catch (Exception e) {
  8. return false;
  9. }
  10. }

检测字符串中是否包含汉字

  1. public static boolean checkChinese(String sequence) {
  2. final String format = "[//u4E00-//u9FA5//uF900-//uFA2D]";
  3. boolean result = false;
  4. Pattern pattern = Pattern.compile(format);
  5. Matcher matcher = pattern.matcher(sequence);
  6. result = matcher.find();
  7. return result;
  8. }

检测字符串中只能包含:中文、数字、下划线(_)、横线(-)

  1. public static boolean checkNickname(String sequence) {
  2. final String format = "[^//u4E00-//u9FA5//uF900-//uFA2D//w-_]";
  3. Pattern pattern = Pattern.compile(format);
  4. Matcher matcher = pattern.matcher(sequence);
  5. return !matcher.find();
  6. }

获取中间有*号的手机号

  1. public static String getPhonePass(String phone) {
  2. if (null == phone || "".equals(phone) || phone.length() < 11) {
  3. return "";
  4. }
  5.  
  6. String passA = phone.substring(0,3);
  7. String passB = phone.substring(phone.length() - 3,phone.length());
  8. return passA + "*****" + passB;
  9. }

获取中间有*号的身份证号

  1. public static String getPidPass(String pid) {
  2. if (null == pid || "".equals(pid) || pid.length() < 18) {
  3. return "";
  4. }
  5.  
  6. String passA = pid.substring(0,3);
  7. String passB = pid.substring(pid.length() - 3,pid.length());
  8. return passA + "*****" + passB;
  9. }

猜你在找的正则表达式相关文章