java正則表達式驗證郵箱、電話號碼示例
下面的代碼使用正則表達式驗證輸入格式包括了驗證郵箱和驗證手機號碼
package com.firewolf.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 使用正則表達式驗證輸入格式
* @author liuxing
*
*/
public class RegexValidateUtil {
public static void main(String[] args) {
System.out.println(checkEmail("14_8@qw.df"));
System.out.println(checkMobileNumber("071-3534452"));
}
/**
* 驗證郵箱
* @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;
}
/**
* 驗證手機號碼
* @param mobiles
* @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;
}
}
PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
相關文章
Spring?Boot中Controller層規(guī)劃與最佳實踐建議
本文將系統(tǒng)性地介紹如何規(guī)劃編寫高質量的Controller層代碼,涵蓋RESTful設計、參數(shù)處理、異常處理、日志記錄、安全控制等關鍵方面,并提供可落地的代碼示例和架構建議,感興趣的朋友一起看看吧2025-06-06ConcurrentMap.putIfAbsent(key,value)用法實例
這篇文章主要介紹了ConcurrentMap.putIfAbsent(key,value)用法實例,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02