当前位置: 移动技术网 > IT编程>开发语言>Java > Java常用正则表达式验证工具类RegexUtils.java

Java常用正则表达式验证工具类RegexUtils.java

2019年07月22日  | 移动技术网IT编程  | 我要评论
正则表达式常常用来验证各种表单,java 表单注册常用正则表达式验证工具类,常用正则表达式大集合。 1. 电话号码 2. 邮编 3. qq 4. e-mail

正则表达式常常用来验证各种表单,java 表单注册常用正则表达式验证工具类,常用正则表达式大集合。

1. 电话号码

2. 邮编

3. qq

4. e-mail

5. 手机号码

6. url

7. 是否为数字

8. 是否为中文

9. 身份证

10. 域名

11. ip 。。。。

常用验证应有尽有! 这的确是您从事 web 开发,服务器端表单验证之良品!你,值得拥有 ^_^

/*
 *  copyright 2012-2013 the haohui network corporation
 */
package com.haohui.common.utils;

import java.util.regex.matcher;
import java.util.regex.pattern;

/**
 * @project baidamei
 * @author cevencheng <cevencheng@gmail.com>
 * @create 2012-11-15 下午4:54:42
 */
public class regexutils {

   /**
   * 验证email
   * @param email email地址,格式:zhangsan@zuidaima.com,zhangsan@xxx.com.cn,xxx代表邮件服务商
   * @return 验证成功返回true,验证失败返回false
   */ 
  public static boolean checkemail(string email) { 
    string regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?"; 
    return pattern.matches(regex, email); 
  } 
   
  /**
   * 验证身份证号码
   * @param idcard 居民身份证号码15位或18位,最后一位可能是数字或字母
   * @return 验证成功返回true,验证失败返回false
   */ 
  public static boolean checkidcard(string idcard) { 
    string regex = "[1-9]\\d{13,16}[a-za-z0-9]{1}"; 
    return pattern.matches(regex,idcard); 
  } 
   
  /**
   * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港))
   * @param mobile 移动、联通、电信运营商的号码段
   *<p>移动的号段:134(0-8)、135、136、137、138、139、147(预计用于td上网卡)
   *、150、151、152、157(td专用)、158、159、187(未启用)、188(td专用)</p>
   *<p>联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g)</p>
   *<p>电信的号段:133、153、180(未启用)、189</p>
   * @return 验证成功返回true,验证失败返回false
   */ 
  public static boolean checkmobile(string mobile) { 
    string regex = "(\\+\\d+)?1[34578]\\d{9}$"; 
    return pattern.matches(regex,mobile); 
  } 
   
  /**
   * 验证固定电话号码
   * @param phone 电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447
   * <p><b>国家(地区) 代码 :</b>标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9 的一位或多位数字,
   * 数字之后是空格分隔的国家(地区)代码。</p>
   * <p><b>区号(城市代码):</b>这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号——
   * 对不使用地区或城市代码的国家(地区),则省略该组件。</p>
   * <p><b>电话号码:</b>这包含从 0 到 9 的一个或多个数字 </p>
   * @return 验证成功返回true,验证失败返回false
   */ 
  public static boolean checkphone(string phone) { 
    string regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$"; 
    return pattern.matches(regex, phone); 
  } 
   
  /**
   * 验证整数(正整数和负整数)
   * @param digit 一位或多位0-9之间的整数
   * @return 验证成功返回true,验证失败返回false
   */ 
  public static boolean checkdigit(string digit) { 
    string regex = "\\-?[1-9]\\d+"; 
    return pattern.matches(regex,digit); 
  } 
   
  /**
   * 验证整数和浮点数(正负整数和正负浮点数)
   * @param decimals 一位或多位0-9之间的浮点数,如:1.23,233.30
   * @return 验证成功返回true,验证失败返回false
   */ 
  public static boolean checkdecimals(string decimals) { 
    string regex = "\\-?[1-9]\\d+(\\.\\d+)?"; 
    return pattern.matches(regex,decimals); 
  } 
   
  /**
   * 验证空白字符
   * @param blankspace 空白字符,包括:空格、\t、\n、\r、\f、\x0b
   * @return 验证成功返回true,验证失败返回false
   */ 
  public static boolean checkblankspace(string blankspace) { 
    string regex = "\\s+"; 
    return pattern.matches(regex,blankspace); 
  } 
   
  /**
   * 验证中文
   * @param chinese 中文字符
   * @return 验证成功返回true,验证失败返回false
   */ 
  public static boolean checkchinese(string chinese) { 
    string regex = "^[\u4e00-\u9fa5]+$"; 
    return pattern.matches(regex,chinese); 
  } 
   
  /**
   * 验证日期(年月日)
   * @param birthday 日期,格式:1992-09-03,或1992.09.03
   * @return 验证成功返回true,验证失败返回false
   */ 
  public static boolean checkbirthday(string birthday) { 
    string regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}"; 
    return pattern.matches(regex,birthday); 
  } 
   
  /**
   * 验证url地址
   * @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80
   * @return 验证成功返回true,验证失败返回false
   */ 
  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); 
  } 
  
  /**
   * <pre>
   * 获取网址 url 的一级域
   * </pre>
   * 
   * @param url
   * @return
   */
  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();
  }
  /**
   * 匹配中国邮政编码
   * @param postcode 邮政编码
   * @return 验证成功返回true,验证失败返回false
   */ 
  public static boolean checkpostcode(string postcode) { 
    string regex = "[1-9]\\d{5}"; 
    return pattern.matches(regex, postcode); 
  } 
   
  /**
   * 匹配ip地址(简单匹配,格式,如:192.168.1.1,127.0.0.1,没有匹配ip段的大小)
   * @param ipaddress ipv4标准地址
   * @return 验证成功返回true,验证失败返回false
   */ 
  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; 
 
/**
 * 验证工具类
 * @author admin
 *
 */
public class validation { 
  //------------------常量定义 
  /** 
   * 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 = "^([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+)+";
  /** 
   * 电话号码正则表达式= (^(\d{2,4}[-_-—]?)?\d{3,8}([-_-—]?\d{3,8})?([-_-—]?\d{1,7})?$)|(^0?1[35]\d{9}$) 
   */
  public static final string phone = "(^(\\d{2,4}[-_-—]?)?\\d{3,8}([-_-—]?\\d{3,8})?([-_-—]?\\d{1,7})?$)|(^0?1[35]\\d{9}$)" ; 
  /** 
   * 手机号码正则表达式=^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$
   */
  public static final string mobile ="^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\\d{8}$"; 
 
  /** 
   * integer正则表达式 ^-?(([1-9]\d*$)|0) 
   */
  public static final string integer = "^-?(([1-9]\\d*$)|0)"; 
  /** 
   * 正整数正则表达式 >=0 ^[1-9]\d*|0$ 
   */
  public static final string integer_negative = "^[1-9]\\d*|0$"; 
  /** 
   * 负整数正则表达式 <=0 ^-[1-9]\d*|0$ 
   */
  public static final string integer_positive = "^-[1-9]\\d*|0$";   
  /** 
   * double正则表达式 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ 
   */
  public static final string double ="^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$"; 
  /** 
   * 正double正则表达式 >=0 ^[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$"; 
  /** 
   * 负double正则表达式 <= 0 ^(-([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$";  
  /** 
   * 年龄正则表达式 ^(?:[1-9][0-9]?|1[01][0-9]|120)$ 匹配0-120岁 
   */
  public static final string age="^(?:[1-9][0-9]?|1[01][0-9]|120)$"; 
  /** 
   * 邮编正则表达式 [0-9]\d{5}(?!\d) 国内6位邮编 
   */
  public static final string code="[0-9]\\d{5}(?!\\d)";  
  /** 
   * 匹配由数字、26个英文字母或者下划线组成的字符串 ^\w+$ 
   */
  public static final string str_eng_num_="^\\w+$"; 
  /** 
   * 匹配由数字和26个英文字母组成的字符串 ^[a-za-z0-9]+$ 
   */
  public static final string str_eng_num="^[a-za-z0-9]+"; 
  /** 
   * 匹配由26个英文字母组成的字符串 ^[a-za-z]+$ 
   */
  public static final string str_eng="^[a-za-z]+$"; 
  /** 
   * 过滤特殊字符串正则 
   * regex="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]"; 
   */
  public static final string str_special="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]"; 
  /*** 
   * 日期正则 支持: 
   * yyyy-mm-dd 
   * yyyy/mm/dd 
   * yyyy_mm_dd 
   * yyyymmdd 
   * yyyy.mm.dd的形式 
   */
  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)$))"; 
  /*** 
   * 日期正则 支持: 
   * yyyy-mm-dd 
   */
  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)";
   
  /** 
   * url正则表达式 
   * 匹配 http www ftp 
   */
  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]$";
   
  /**
   * 匹配数字组成的字符串 ^[0-9]+$ 
   */
  public static final string str_num = "^[0-9]+$"; 
    
////------------------验证方法    
  /** 
   * 判断字段是否为空 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static synchronized boolean strisnull(string str) { 
    return null == str || str.trim().length() <= 0 ? true : false ; 
  } 
  /** 
   * 判断字段是非空 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean strnotnull(string str) { 
    return !strisnull(str) ; 
  } 
  /** 
   * 字符串null转空 
   * @param str 
   * @return boolean 
   */
  public static string nulltostr(string str) {
    return strisnull(str)?"":str; 
  }   
  /** 
   * 字符串null赋值默认值 
   * @param str  目标字符串 
   * @param defaut 默认值 
   * @return string 
   */
  public static string nulltostr(string str,string defaut) { 
    return strisnull(str)?defaut:str; 
  } 
  /** 
   * 判断字段是否为email 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean isemail(string str) { 
    return regular(str,email); 
  } 
  /** 
   * 判断是否为电话号码 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean isphone(string str) { 
    return regular(str,phone); 
  } 
  /** 
   * 判断是否为手机号码 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean ismobile(string str) { 
    return regular(str,mobile); 
  } 
  /** 
   * 判断是否为url 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean isurl(string str) { 
    return regular(str,url); 
  }   
  /** 
   * 判断字段是否为数字 正负整数 正负浮点数 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean isnumber(string str) { 
    return regular(str,double); 
  } 
  /** 
   * 判断字段是否为integer 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean isinteger(string str) { 
    return regular(str,integer); 
  } 
  /** 
   * 判断字段是否为正整数正则表达式 >=0 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean isinteger_negative(string str) { 
    return regular(str,integer_negative); 
  } 
  /** 
   * 判断字段是否为负整数正则表达式 <=0 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean isinteger_positive(string str) { 
    return regular(str,integer_positive); 
  }   
  /** 
   * 判断字段是否为double 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean isdouble(string str) { 
    return regular(str,double); 
  } 
  /** 
   * 判断字段是否为正浮点数正则表达式 >=0 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean isdouble_negative(string str) { 
    return regular(str,double_negative); 
  } 
  /** 
   * 判断字段是否为负浮点数正则表达式 <=0 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean isdouble_positive(string str) { 
    return regular(str,double_positive); 
  }   
  /** 
   * 判断字段是否为日期 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean isdate(string str) { 
    return regular(str,date_all); 
  } 
  /**
   * 验证2010-12-10
   * @param str
   * @return
   */
  public static boolean isdate1(string str) { 
    return regular(str,date_format1); 
  } 
  /** 
   * 判断字段是否为年龄 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean isage(string str) { 
    return regular(str,age) ; 
  } 
  /** 
   * 判断字段是否超长 
   * 字串为空返回fasle, 超过长度{leng}返回ture 反之返回false 
   * @param str 
   * @param leng 
   * @return boolean 
   */
  public static boolean islengout(string str,int leng) {    
    return strisnull(str)?false:str.trim().length() > leng ; 
  } 
  /** 
   * 判断字段是否为身份证 符合返回ture 
   * @param str 
   * @return boolean 
   */
  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 ; 
    } 
      
  } 
  /** 
   * 判断字段是否为邮编 符合返回ture 
   * @param str 
   * @return boolean 
   */
  public static boolean iscode(string str) { 
    return regular(str,code) ; 
  } 
  /** 
   * 判断字符串是不是全部是英文字母 
   * @param str 
   * @return boolean 
   */
  public static boolean isenglish(string str) { 
    return regular(str,str_eng) ; 
  } 
  /** 
   * 判断字符串是不是全部是英文字母+数字 
   * @param str 
   * @return boolean 
   */
  public static boolean iseng_num(string str) { 
    return regular(str,str_eng_num) ; 
  } 
  /** 
   * 判断字符串是不是全部是英文字母+数字+下划线 
   * @param str 
   * @return boolean 
   */
  public static boolean iseng_num_(string str) { 
    return regular(str,str_eng_num_) ; 
  } 
  /** 
   * 过滤特殊字符串 返回过滤后的字符串 
   * @param str 
   * @return boolean 
   */
  public static string filterstr(string str) { 
    pattern p = pattern.compile(str_special); 
    matcher m = p.matcher(str); 
    return  m.replaceall("").trim(); 
  }
   
  /**
   * 校验机构代码格式
   * @return
   */
  public static boolean isjigoucode(string str){
    return regular(str,jigou_code) ; 
  }
   
  /** 
   * 判断字符串是不是数字组成 
   * @param str 
   * @return boolean 
   */
  public static boolean isstr_num(string str) { 
    return regular(str,str_num) ; 
  } 
   
  /** 
   * 匹配是否符合正则表达式pattern 匹配返回true 
   * @param str 匹配的字符串 
   * @param pattern 匹配模式 
   * @return boolean 
   */
  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(); 
  } 
   
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网