Java 正则表达式 工具类 中文 英文 email 手机号 身份证 数字 日期

大家好,我是烤鸭:

    分享一个Java版本的正则校验工具类。

Validation.java

package com.xxx.xxx.common.utils;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]|19[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]+$";  /*** 最多10个汉字:*/public static final String STR_CHN_TEN="^[\\u4e00-\\u9fa5]{0,10}";/*** 过滤特殊字符串正则 * 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) ;  } /*** 判断字符串是不是2-10个中文组成* @param str* @return boolean*/public static boolean isSTR_CHN_TEN(String str) {return Regular(str,STR_CHN_TEN) ;}/** * 匹配是否符合正则表达式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();  }  }  

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/412924.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[css] position跟margin collapse这些特性相互叠加后会怎么样?

[css] position跟margin collapse这些特性相互叠加后会怎么样&#xff1f; 怎样防止外边距重叠&#xff1f;底部元素脱离文档流(float: left/right || position: absolute/fixed)个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一…

Python_全局变量的定义

1.在my套件下新建一个关键字systemkey并进行脚本的编写&#xff1a;创建一个${var1}变量&#xff0c;并赋值为aaaaaaaaaa Set Global Variable ${var1} aaaaaaaaaa 2.在全局&#xff08;最顶部文件夹&#xff09;中Suite Setup→Edit→systemkey&#xff08…

Tomcat设置URL兼容特殊符号

catalina.properties配置&#xff1a; tomcat.util.http.parser.HttpParser.requestTargetAllow|{}[] org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASHtrueserver.xml配置&#xff1a; <Connector port"80" protocol"HTTP/1.1"connectionTi…

[css] 移动端的布局用过媒体查询吗?写一个试试

[css] 移动端的布局用过媒体查询吗&#xff1f;写一个试试 使用过Demo<style>.demo {width: 100px;height: 100px;background: #000000;}media only screen and (min-width: 680px ) {.demo {background: red;}} </style> <div class"demo"></d…

java 实现 常见排序算法(一) 冒泡排序

大家好&#xff0c;我是烤鸭&#xff1a; 今天分享一下基础排序算法之冒泡排序。 1. 冒泡排序&#xff1a; 原理&#xff1a;比较两个相邻的元素&#xff0c;将较大的元素交换至右端。 思路&#xff1a;依次比较相邻的两个数&#xff0c;将小数放在前面&#xff0c;大…

vue学习之npm

任何一门计算机语言都包含了丰富的第三方库&#xff0c;npm就是JavaScript这门语言的第三方库管理工具&#xff0c;本文详细介绍了JavaScript的包管理工具&#xff0c;npm。 在计算机中安装好Node.js之后&#xff0c;默认已经安装好了npm包管理工具&#xff0c;我们可以输入npm…

idea的maven project消失解决方案

方案一&#xff1a;点击菜单栏View->Tool Windows->Maven projects 方案二&#xff1a;File -> Settings -> Plugins -> Installed ->找到下面两个并勾选->然后重启 1&#xff0c;Maven Integration 2&#xff0c;Maven Integration Extension

[css] 举例说明你知道的css技巧有哪些?

[css] 举例说明你知道的css技巧有哪些&#xff1f; /* 等比例容器 */ .ratio { position: relative; display: block; } .ratio:before { content: ; padding-top: 100%; float: left; } .ratio::after { content: ; display: block; clear: both; }/* 非表单元素也适用的 dis…

Java 深copy 浅copy 引用copy

大家好&#xff0c;我是烤鸭&#xff1a; 今天分享一下浅copy和深copy。 1. 深copy 什么是深copy&#xff0c;只复制原对象属性值&#xff0c;不管地址。 说一下业务场景&#xff1a; 如果我想创建一个对象&#xff0c;只是对原对象的某个属性值改变。普通的做法就是new 一个…

ubuntu设置root密码及 Xftp连接linux(ubuntu)时提示ssh服务器拒绝了密码,请再试一次...

ubuntu设置root密码及 Xftp连接linux&#xff08;ubuntu&#xff09;时提示ssh服务器拒绝了密码&#xff0c;请再试一次 原文:ubuntu设置root密码及 Xftp连接linux&#xff08;ubuntu&#xff09;时提示ssh服务器拒绝了密码&#xff0c;请再试一次altf2,在弹出的运行窗口中输入…

[css] 判断第一行和第二行的颜色分别是什么?并解释为什么?

[css] 判断第一行和第二行的颜色分别是什么&#xff1f;并解释为什么&#xff1f; <style> .red {color:red;} .green {color:green;} </style><div class"red green">第一行&#xff1a;颜色是什么&#xff1f;</div> <div class"…

linux定时任务清理cache缓存

大家好&#xff0c;我是烤鸭&#xff1a; 如果你出现类似cache过多的情况&#xff0c;请参考这篇。 buff/cache 占了1.6G&#xff0c;多数情况下是无所谓的。但是有时候在系统内存不足的时候&#xff0c;可能会影响其他程序的执行。 之前就遇到过 jenkins 因为内存不足 集成失败…

SecureCRT Application 已停止工作

解决方法一&#xff1a; cmd ----> regedit —>HKEY_LOCAL_MACHINE\SOFTWARE\vandyke 删掉vandyke 解决方法二&#xff1a; SecureCRT使用过程中出现异常后自动关闭&#xff0c;导致下次无法正常启动&#xff08;运行程序无反应&#xff09;&#xff0c;此时一种可能的原…

SQLServer之事务简介

事务定义 事务是单个的工作单元。事务是在数据库上按照一定的逻辑顺序执行的任务序列&#xff0c;既可以由用户手动执行&#xff0c;也可以由某种数据库程序自动执行。 事务分类 自动提交事务 每条单独的语句都是一个事务。 在自动提交模式下&#xff0c; 每个数据库操作是在执…

[css] 用css怎么实现两端对齐?

[css] 用css怎么实现两端对齐&#xff1f; 文本的两端对齐<style>.form .text {display: inline-block;width: 65px;text-align-last: justify;}</style><div class"form"><div><span class"text">用户名</span><i…

springboot mybatis mysql8.0 异常 invalid bound statement (not found)(汇总篇)

大家好&#xff0c;我是烤鸭&#xff1a; 现在是采坑实录。 invalid bound statement (not found) 这个坑用过mybatis的人肯定都踩过。 1. 环境 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</ar…

Vmware centos无法连接网络

在vmware下安装了centos7&#xff0c;桥接模式&#xff0c;无法连接网络 解决方案一&#xff1a; 然后重启虚拟机 解决方案二&#xff1a; 然后再试试重启能否联网 解决方案三&#xff1a;

[css] 你有用过弹性布局吗?说说你对它的理解

[css] 你有用过弹性布局吗&#xff1f;说说你对它的理解 按我的经验在样式规律上大致分下类吧&#xff0c;等宽弹性布局单元素弹性布局多元素定比弹性布局等隙布局用 flex 或 grid 能非常轻松的完成以上效果&#xff0c;而 css2 时代多半还需要更多元素嵌套才能完成了。所谓弹…

洛谷P2015 二叉苹果树

题目描述 有一棵苹果树&#xff0c;如果树枝有分叉&#xff0c;一定是分2叉&#xff08;就是说没有只有1个儿子的结点&#xff09; 这棵树共有N个结点&#xff08;叶子点或者树枝分叉点&#xff09;&#xff0c;编号为1-N,树根编号一定是1。 我们用一根树枝两端连接的结点的编号…

mybatis 一对一 一对多 级联查询

大家好&#xff0c;我是烤鸭&#xff1a; 今天分享一下关于mybatis的级联查询。 环境&#xff1a; mybatis 3.2.8 spring 4.1.9 1. 业务场景 在一个人申请某些账号或者权限的时候&#xff0c;比如微信的认证流程。 会让你一步一步按要求输入&#xff0c;比如第一步&am…