正则表达式常常用来验证各种表单,Java 表单注册常用正则表达式验证工具类,常用正则表达式大集合。
1. 电话号码
2. 邮编
3. QQ
4. E-mail
5. 手机号码
6. URL
7. 是否为数字
8. 是否为中文
9. 身份证
10. 域名
11. IP 。。。。
常用验证应有尽有! 这的确是您从事 web 开发,服务器端表单验证之良品!你,值得拥有 ^_^
package com.haohui.common.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexUtils {
public static boolean checkEmail(String email) {
String regex = "\w+@\w+\.[a-z]+(\.[a-z]+)?";
return Pattern.matches(regex, email);
}
public static boolean checkIdCard(String idCard) {
String regex = "[1-9]\d{13,16}[a-zA-Z0-9]{1}";
return Pattern.matches(regex,idCard);
}
public static boolean checkMobile(String mobile) {
String regex = "(\+\d+)?1[34578]\d{9}$";
return Pattern.matches(regex,mobile);
}
public static boolean checkPhone(String phone) {
String regex = "(\+\d+)?(\d{3,4}\-?)?\d{7,8}$";
return Pattern.matches(regex, phone);
}
public static boolean checkDigit(String digit) {
String regex = "\-?[1-9]\d+";
return Pattern.matches(regex,digit);
}
public static boolean checkDecimals(String decimals) {
String regex = "\-?[1-9]\d+(\.\d+)?";
return Pattern.matches(regex,decimals);
}
public static boolean checkBlankSpace(String blankSpace) {
String regex = "\s+";
return Pattern.matches(regex,blankSpace);
}
public static boolean checkChinese(String chinese) {
String regex = "^[u4E00-u9FA5]+$";
return Pattern.matches(regex,chinese);
}
public static boolean checkBirthday(String birthday) {
String regex = "[1-9]{4}([-./])\d{1,2}\1\d{1,2}";
return Pattern.matches(regex,birthday);
}
public static boolean checkURL(String url) {
String regex = "(https?://(w{3}\.)?)?\w+\.\w+(\.[a-zA-Z]+)*(:\d{1,5})?(/\w*)*(\??(.+=.*)?(&.+=.*)?)?";
return Pattern.matches(regex, url);
}
public static String getDomain(String url) {
Pattern p = Pattern.compile("(?<=http://|\.)[^.]*?\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
// 获取完整的域名
// Pattern p=Pattern.compile("[^//]*?\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(url);
matcher.find();
return matcher.group();
}
public static boolean checkPostcode(String postcode) {
String regex = "[1-9]\d{5}";
return Pattern.matches(regex, postcode);
}
public static boolean checkIpAddress(String ipAddress) {
String regex = "[1-9](\d{1,2})?\.(0|([1-9](\d{1,2})?))\.(0|([1-9](\d{1,2})?))\.(0|([1-9](\d{1,2})?))";
return Pattern.matches(regex, ipAddress);
}
}
分享一个用正则表达式校验电话号码、身份证号、日期格式、URL、Email等等格式的工具类
package com.eabax.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Validation {
//------------------常量定义
//public static final String EMAIL = "^([a-z0-9A-Z]+[-|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,}$";;
public static final String EMAIL = "\w+(\.\w+)*@\w+(\.\w+)+";
public static final String PHONE = "(^(\d{2,4}[-_-—]?)?\d{3,8}([-_-—]?\d{3,8})?([-_-—]?\d{1,7})?$)|(^0?1[35]\d{9}$)" ;
public static final String MOBILE ="^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$";
public static final String INTEGER = "^-?(([1-9]\d*$)|0)";
public static final String INTEGER_NEGATIVE = "^[1-9]\d*|0$";
public static final String INTEGER_POSITIVE = "^-[1-9]\d*|0$";
public static final String DOUBLE ="^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$";
public static final String DOUBLE_NEGATIVE ="^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$";
public static final String DOUBLE_POSITIVE ="^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$";
public static final String AGE="^(?:[1-9][0-9]?|1[01][0-9]|120)$";
public static final String CODE="[0-9]\d{5}(?!\d)";
public static final String STR_ENG_NUM_="^\w+$";
public static final String STR_ENG_NUM="^[A-Za-z0-9]+";
public static final String STR_ENG="^[A-Za-z]+$";
public static final String STR_SPECIAL="[`~!@#$%^&*()+=|{}':;',\[\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]";
public static final String DATE_ALL="((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._]?)(10|12|0?[13578])([-\/\._]?)(3[01]|[12][0-9]|0?[1-9])$)" +
"|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._]?)(11|0?[469])([-\/\._]?)(30|[12][0-9]|0?[1-9])$)" +
"|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._]?)(0?2)([-\/\._]?)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._]?)(0?2)([-\/\._]?)(29)$)|(^([3579][26]00)" +
"([-\/\._]?)(0?2)([-\/\._]?)(29)$)" +
"|(^([1][89][0][48])([-\/\._]?)(0?2)([-\/\._]?)(29)$)|(^([2-9][0-9][0][48])([-\/\._]?)" +
"(0?2)([-\/\._]?)(29)$)" +
"|(^([1][89][2468][048])([-\/\._]?)(0?2)([-\/\._]?)(29)$)|(^([2-9][0-9][2468][048])([-\/\._]?)(0?2)" +
"([-\/\._]?)(29)$)|(^([1][89][13579][26])([-\/\._]?)(0?2)([-\/\._]?)(29)$)|" +
"(^([2-9][0-9][13579][26])([-\/\._]?)(0?2)([-\/\._]?)(29)$))";
public static final String DATE_FORMAT1="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
public static final String URL = "^(http|www|ftp|)?(://)?(\w+(-\w+)*)(\.(\w+(-\w+)*))*((:\d+)?)(/(\w+(-\w+)*))*(\.?(\w)*)(\?)?" +
"(((\w*%)*(\w*\?)*(\w*:)*(\w*\+)*(\w*\.)*(\w*&)*(\w*-)*(\w*=)*(\w*%)*(\w*\?)*" +
"(\w*:)*(\w*\+)*(\w*\.)*" +
"(\w*&)*(\w*-)*(\w*=)*)*(\w*)*)$";
public static final String IDCARD="((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})" +
"(([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}" +
"[Xx0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3}))";
public static final String JIGOU_CODE = "^[A-Z0-9]{8}-[A-Z0-9]$";
public static final String STR_NUM = "^[0-9]+$";
////------------------验证方法
public static synchronized boolean StrisNull(String str) {
return null == str || str.trim().length() <= 0 ? true : false ;
}
public static boolean StrNotNull(String str) {
return !StrisNull(str) ;
}
public static String nulltoStr(String str) {
return StrisNull(str)?"":str;
}
public static String nulltoStr(String str,String defaut) {
return StrisNull(str)?defaut:str;
}
public static boolean isEmail(String str) {
return Regular(str,EMAIL);
}
public static boolean isPhone(String str) {
return Regular(str,PHONE);
}
public static boolean isMobile(String str) {
return Regular(str,MOBILE);
}
public static boolean isUrl(String str) {
return Regular(str,URL);
}
public static boolean isNumber(String str) {
return Regular(str,DOUBLE);
}
public static boolean isInteger(String str) {
return Regular(str,INTEGER);
}
public static boolean isINTEGER_NEGATIVE(String str) {
return Regular(str,INTEGER_NEGATIVE);
}
public static boolean isINTEGER_POSITIVE(String str) {
return Regular(str,INTEGER_POSITIVE);
}
public static boolean isDouble(String str) {
return Regular(str,DOUBLE);
}
public static boolean isDOUBLE_NEGATIVE(String str) {
return Regular(str,DOUBLE_NEGATIVE);
}
public static boolean isDOUBLE_POSITIVE(String str) {
return Regular(str,DOUBLE_POSITIVE);
}
public static boolean isDate(String str) {
return Regular(str,DATE_ALL);
}
public static boolean isDate1(String str) {
return Regular(str,DATE_FORMAT1);
}
public static boolean isAge(String str) {
return Regular(str,AGE) ;
}
public static boolean isLengOut(String str,int leng) {
return StrisNull(str)?false:str.trim().length() > leng ;
}
public static boolean isIdCard(String str) {
if(StrisNull(str)) return false ;
if(str.trim().length() == 15 || str.trim().length() == 18) {
return Regular(str,IDCARD);
}else {
return false ;
}
}
public static boolean isCode(String str) {
return Regular(str,CODE) ;
}
public static boolean isEnglish(String str) {
return Regular(str,STR_ENG) ;
}
public static boolean isENG_NUM(String str) {
return Regular(str,STR_ENG_NUM) ;
}
public static boolean isENG_NUM_(String str) {
return Regular(str,STR_ENG_NUM_) ;
}
public static String filterStr(String str) {
Pattern p = Pattern.compile(STR_SPECIAL);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}
public static boolean isJigouCode(String str){
return Regular(str,JIGOU_CODE) ;
}
public static boolean isSTR_NUM(String str) {
return Regular(str,STR_NUM) ;
}
private static boolean Regular(String str,String pattern){
if(null == str || str.trim().length()<=0)
return false;
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str);
return m.matches();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



