public class RegxUtils {
//------------------常量定义
/**
* 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();
} public static void main(String[] args) {
boolean age = RegxUtils.isAge("100");
System.out.println(age);
}
}

RegxUtils正则表达式工具类的更多相关文章

  1. JAVA自动生成正则表达式工具类

    经过很久的努力,终于完成了JAVA自动生成正则表达式工具类.还记得之前需要正则,老是从网上找吗?找了想修改也不会修改.现在不用再为此烦恼了,使用此生成类轻松搞定所有正则表达式.赶快在同事面前炫一下吧. ...

  2. 正则表达式工具类,正则表达式封装,Java正则表达式

    正则表达式工具类 正则表达式封装 Java正则表达式 >>>>>>>>>>>>>>>>>>& ...

  3. [Swift]正则表达式工具类

    正则表达式工具类 import Foundation //基于NSRegularExpression api 的正则处理工具类 public struct Regex { private let re ...

  4. Android 中正则表达式工具类

    package com.example.administrator.magiclamp.utils; import java.util.regex.Pattern; /** * 校验器:利用正则表达式 ...

  5. Java常用工具类之RegexpUtils,正则表达式工具类

    package com.test.core.util; import org.apache.log4j.Logger; import org.apache.oro.text.regex.Malform ...

  6. Android常用正则工具类

    此类提供日常开发中常用的正则验证函数,比如:邮箱.手机号.电话号码.身份证号码.日期.数字.小数.URL.IP地址等.使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于:   ...

  7. 实用工具类--第三方开源--Lazy

    下载地址 :https://github.com/ddwhan0123/Lazy 工具 描述 AnimationUtils 动画工具类 AppUtils APP相关信息工具类 AssetDatabas ...

  8. .net使用正则表达式校验、匹配字符工具类

    开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...

  9. 正则表达式验证工具类RegexUtils.java

    Java 表单注册常用正则表达式验证工具类,常用正则表达式大集合. 1. 电话号码 2. 邮编 3. QQ 4. E-mail 5. 手机号码 6. URL 7. 是否为数字 8. 是否为中文 9. ...

随机推荐

  1. [C8] 聚类(Clustering)

    聚类(Clustering) 非监督学习:简介(Unsupervised Learning: Introduction) 本章节介绍聚类算法,这是我们学习的第一个非监督学习算法--学习无标签数据,而不 ...

  2. Dockerfil

    Dockerfile简介 dockerfile 是一个文本格式的配置文件, 用户可以使用 Dockerfile 来快速创建自定义的镜像,另外,使用Dockerfile去构建镜像好比使用pom去构建ma ...

  3. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

  4. 研究是一门艺术 (韦恩·C·布斯, 格雷戈里·G·卡洛姆, 约瑟夫·M·威廉姆斯 著)

    第一部分 研究,研究者与读者 前言: 开始一个研究计划 (已看) 第一章 以书面形式来思考 (已看) 第二章 与读者建立联系 第二部分 提问题,找答案 前言: 规划你的研究计划 第三章 从题目到问题 ...

  5. Paper | Xception: Deep Learning with Depthwise Separable Convolutions

    目录 故事 Inception结构和思想 更进一步,以及现有的深度可分离卷积 Xception结构 实验 这篇论文写得很好.只要你知道卷积操作或公式,哪怕没看过Inception,也能看懂. 核心贡献 ...

  6. 洛谷P4015 运输问题 网络流24题

    看了下SPFA题解,一个一个太麻烦了,另一个写的很不清楚,而且注释都变成了"????"不知道怎么过的,于是自己来一发SPFA算法. Part 1.题意 M 个仓库,卖给 N 个商店 ...

  7. HTTP协议第一篇:三握手解读

    TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位,有6种标 ...

  8. 物联网架构成长之路(43)-k8s从入门到放弃

    0. 前言 这段时间要入门一下CI/CD了,以前简单的了解过Jenkins,现在要把以下的这个图的架构搭建起来.国外可能一两个命令就安装完成的事情,我折腾了2天多,真的差点放弃了. 1. 安装Virt ...

  9. Jvisualvm简单使用教程

    本博客介绍一下jvisualvm的简单使用教程,jvisualvm功能还是挺多的,不过本博客之简单介绍一下 1.拿线程快照信息 在jdk安装目录找到jvisualvm.exe,${JDK_HOME}\ ...

  10. 使用NumPy、Numba的简单使用(一)

    Numpy是python的一个三方库,主要是用于计算的,数组的算数和逻辑运算.与线性代数有关的操作. 很多情况下,我们可以与SciPy和 Matplotlib(绘图库)一起使用.来替代MatLab,下 ...