正则工具类 -- RegexUtils
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class RegexUtils { /**
* 正则:手机号(简单)
*/
public static final String REGEX_MOBILE_SIMPLE = "^[1]\\d{10}$";
/**
* 正则:手机号(精确)
* <p>移动:134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188</p>
* <p>联通:130、131、132、145、155、156、175、176、185、186</p>
* <p>电信:133、153、173、177、180、181、189</p>
* <p>全球星:1349</p>
* <p>虚拟运营商:170</p>
*/
public static final String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}$";
/**
* 正则:电话号码
*/
public static final String REGEX_TEL = "^0\\d{2,3}[- ]?\\d{7,8}";
/**
* 正则:身份证号码15位
*/
public static final String REGEX_ID_CARD15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
/**
* 正则:身份证号码18位
*/
public static final String REGEX_ID_CARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";
/**
* 正则:邮箱
*/
public static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
/**
* 正则:URL
*/
public static final String REGEX_URL = "[a-zA-z]+://[^\\s]*";
/**
* 正则:汉字
*/
public static final String REGEX_ZH = "^[\\u4e00-\\u9fa5]+$";
/**
* 正则:用户名,取值范围为a-z,A-Z,0-9,"_",汉字,不能以"_"结尾,用户名必须是6-20位
*/
public static final String REGEX_USERNAME = "^[\\w\\u4e00-\\u9fa5]{6,20}(?<!_)$";
/**
* 正则:yyyy-MM-dd格式的日期校验,已考虑平闰年
*/
public static final String REGEX_DATE = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";
/**
* 正则:IP地址
*/
public static final String REGEX_IP = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"; /**
* 正则:双字节字符(包括汉字在内)
*/
public static final String REGEX_DOUBLE_BYTE_CHAR = "[^\\x00-\\xff]";
/**
* 正则:空白行
*/
public static final String REGEX_BLANK_LINE = "\\n\\s*\\r";
/**
* 正则:QQ号
*/
public static final String REGEX_TENCENT_NUM = "[1-9][0-9]{4,}";
/**
* 正则:中国邮政编码
*/
public static final String REGEX_ZIP_CODE = "[1-9]\\d{5}(?!\\d)";
/**
* 正则:正整数
*/
public static final String REGEX_POSITIVE_INTEGER = "^[1-9]\\d*$";
/**
* 正则:负整数
*/
public static final String REGEX_NEGATIVE_INTEGER = "^-[1-9]\\d*$";
/**
* 正则:整数
*/
public static final String REGEX_INTEGER = "^-?[1-9]\\d*$";
/**
* 正则:非负整数(正整数 + 0)
*/
public static final String REGEX_NOT_NEGATIVE_INTEGER = "^[1-9]\\d*|0$";
/**
* 正则:非正整数(负整数 + 0)
*/
public static final String REGEX_NOT_POSITIVE_INTEGER = "^-[1-9]\\d*|0$";
/**
* 正则:正浮点数
*/
public static final String REGEX_POSITIVE_FLOAT = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$";
/**
* 正则:负浮点数
*/
public static final String REGEX_NEGATIVE_FLOAT = "^-[1-9]\\d*\\.\\d*|-0\\.\\d*[1-9]\\d*$"; private RegexUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
} /**
* 验证手机号(简单)
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isMobileSimple(CharSequence input) {
return isMatch(REGEX_MOBILE_SIMPLE, input);
} /**
* 验证手机号(精确)
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isMobileExact(CharSequence input) {
return isMatch(REGEX_MOBILE_EXACT, input);
} /**
* 验证电话号码
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isTel(CharSequence input) {
return isMatch(REGEX_TEL, input);
} /**
* 验证身份证号码15位
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isIDCard15(CharSequence input) {
return isMatch(REGEX_ID_CARD15, input);
} /**
* 验证身份证号码18位
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isIDCard18(CharSequence input) {
return isMatch(REGEX_ID_CARD18, input);
} /**
* 验证邮箱
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isEmail(CharSequence input) {
return isMatch(REGEX_EMAIL, input);
} /**
* 验证URL
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isURL(CharSequence input) {
return isMatch(REGEX_URL, input);
} /**
* 验证汉字
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isZh(CharSequence input) {
return isMatch(REGEX_ZH, input);
} /**
* 验证用户名
* <p>取值范围为a-z,A-Z,0-9,"_",汉字,不能以"_"结尾,用户名必须是6-20位</p>
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isUsername(CharSequence input) {
return isMatch(REGEX_USERNAME, input);
} /**
* 验证yyyy-MM-dd格式的日期校验,已考虑平闰年
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isDate(CharSequence input) {
return isMatch(REGEX_DATE, input);
} /**
* 验证IP地址
*
* @param input 待验证文本
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isIP(CharSequence input) {
return isMatch(REGEX_IP, input);
} /**
* 判断是否匹配正则
*
* @param regex 正则表达式
* @param input 要匹配的字符串
* @return {@code true}: 匹配<br>{@code false}: 不匹配
*/
public static boolean isMatch(String regex, CharSequence input) {
return input != null && input.length() > 0 && Pattern.matches(regex, input);
} /**
* 获取正则匹配的部分
*
* @param regex 正则表达式
* @param input 要匹配的字符串
* @return 正则匹配的部分
*/
public static List<String> getMatches(String regex, CharSequence input) {
if (input == null) return null;
List<String> matches = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
matches.add(matcher.group());
}
return matches;
} /**
* 获取正则匹配分组
*
* @param input 要分组的字符串
* @param regex 正则表达式
* @return 正则匹配分组
*/
public static String[] getSplits(String input, String regex) {
if (input == null) return null;
return input.split(regex);
} /**
* 替换正则匹配的第一部分
*
* @param input 要替换的字符串
* @param regex 正则表达式
* @param replacement 代替者
* @return 替换正则匹配的第一部分
*/
public static String getReplaceFirst(String input, String regex, String replacement) {
if (input == null) return null;
return Pattern.compile(regex).matcher(input).replaceFirst(replacement);
} /**
* 替换所有正则匹配的部分
*
* @param input 要替换的字符串
* @param regex 正则表达式
* @param replacement 代替者
* @return 替换所有正则匹配的部分
*/
public static String getReplaceAll(String input, String regex, String replacement) {
if (input == null) return null;
return Pattern.compile(regex).matcher(input).replaceAll(replacement);
}
}
原文:http://www.jianshu.com/p/583998f435d0
正则工具类 -- RegexUtils的更多相关文章
- Android常用正则工具类
此类提供日常开发中常用的正则验证函数,比如:邮箱.手机号.电话号码.身份证号码.日期.数字.小数.URL.IP地址等.使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于: ...
- android经常使用正则工具类
此类提供日常开发中经常使用的正则验证函数.比方:邮箱.手机号.电话号码.身份证号码.日期.数字.小数.URL.IP地址等.使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于: ...
- 正则工具类以及FinalClass
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jadyer/article/details/27811103 完整版见https://jadyer. ...
- 正则表达式验证工具类RegexUtils.java
Java 表单注册常用正则表达式验证工具类,常用正则表达式大集合. 1. 电话号码 2. 邮编 3. QQ 4. E-mail 5. 手机号码 6. URL 7. 是否为数字 8. 是否为中文 9. ...
- C#常用的正则工具类写法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Java常用正则表达式验证工具类RegexUtils.java
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexUtils{ /** * 正则表达式 ...
- Java学习-049-正则工具类
自去年九月份决定再次入学和职业资格进阶,开始备战二者考试至今,以及当下进行中的职称申请,犹如孤独的狼,不断前行在路上,而今凡凡总总的已历8月... 不感慨了,如下为一园友需要的正则工具类,直接上码: ...
- [Swift]正则表达式工具类
正则表达式工具类 import Foundation //基于NSRegularExpression api 的正则处理工具类 public struct Regex { private let re ...
- (转载)详解7.0带来的新工具类:DiffUtil
[Android]详解7.0带来的新工具类:DiffUtil 标签: diffutil 2017-04-17 18:21 226人阅读 评论(0) 收藏 举报 分类: Android学习笔记(94) ...
随机推荐
- Charles破解(转)
NB的Charles是一款付费软件.但…本文将讲解如何破解Charles.注:虽然与文章内容相悖,但还是希望大家能购买正版软件,毕竟都是做软件开发的,何必自断生路,要有版权意识. 环境信息: Mac ...
- javascript预编译和执行过程总结
javascript相对于其它语言来说是一种弱类型的语言,在其它如java语言中,程序的执行需要有编译的阶段,而在javascript中也有类似的“预编译阶段”(javascript的预编译是以代码块 ...
- MongoDB 学习(三)MongoDB 和 Spring 整合(Maven)
一.MongoDB 和 Spring 整合(Maven) 1.相关 jar 包准备 2.用 Maven 创建项目,pom.xml 文件 <project xmlns="http://m ...
- HDU 1561 The more, The Better 经典树形DP
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- js小数转分数-近似递归
精度为小数两位,提高精度可把 toFixed(2)和100同时修改: function decimalsToFractional(decimals){ const formatDecimals = d ...
- 警告: The web application [ROOT] appears to have started a thread named [Thread-48] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
1. 问题描述 tomcat跑web项目(其中依赖java项目) 出现大量上述警告 项目起不来 关键字 memory leak 内存泄漏 2. 解决方案 难道是程序写的有问题? 最终 将tomcat ...
- 【代码笔记】Java常识性基础补充(二)——数组、ArrayList集合、ASCII编码、 eclipse基础操作、eclipse调试、eclipse项目导入、eclipse快捷键
1.0 如何定义数组以及访问数组中的元素,如下所示.ArrayDemo01.java public class ArrayDemo01 { public static void main(String ...
- 模块—— 序列化模块、random模块、os模块 、 sys模块、hashlib模块、collections模块
今天我们来说说Python中的模块: 第三方模块 可以下载/安装/使用 第一步:将pip.exe 所在的目录添加到环境变量中第二步:输入pip第三步:pip install 要安装的模块名称 #pi ...
- 关于background定位
直到刚刚我才发现我小瞧了background定位 因为项目里需要显示隐藏的按钮上有两个图标 开始想了几种方法都不行,然后突然就想到了background定位 果断试了一下 <input type ...
- CSS3及JS简单实现选项卡效果(适配手机端和pc端)
想要适配手机端和pc端,有几种简单的方法,本人使用的是百分比分配的方法. *{ padding: 0; margin: 0; } body,html{ width: 100%; height: 100 ...