package org.linlinjava.litemall.core.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* RegexUtil类的代码是来自[AndroidUtilCode](https://github.com/Blankj/AndroidUtilCode)的RegexUtils类和RegexConstants类
* https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/util/RegexUtils.java
* https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/constant
* /RegexConstants.java
*/
public class RegexUtil { /**
* Regex of simple mobile.
*/
public static final String REGEX_MOBILE_SIMPLE = "^[1]\\d{10}$";
/**
* Regex of exact mobile.
* <p>china mobile: 134(0-8), 135, 136, 137, 138, 139, 147, 150, 151, 152, 157, 158, 159, 178, 182, 183, 184,
* 187, 188, 198</p>
* <p>china unicom: 130, 131, 132, 145, 155, 156, 166, 171, 175, 176, 185, 186</p>
* <p>china telecom: 133, 153, 173, 177, 180, 181, 189, 199</p>
* <p>global star: 1349</p>
* <p>virtual operator: 170</p>
*/
public static final String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(16[6])|(17[0,1,3,5-8])|" +
"(18[0-9])|(19[8,9]))\\d{8}$";
/**
* Regex of telephone number.
*/
public static final String REGEX_TEL = "^0\\d{2,3}[- ]?\\d{7,8}";
/**
* Regex of id card number which length is 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}$";
/**
* Regex of id card number which length is 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])$";
/**
* Regex of email.
*/
public static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
/**
* Regex of url.
*/
public static final String REGEX_URL = "[a-zA-z]+://[^\\s]*";
/**
* Regex of Chinese character.
*/
public static final String REGEX_ZH = "^[\\u4e00-\\u9fa5]+$";
/**
* Regex of username.
* <p>scope for "a-z", "A-Z", "0-9", "_", "Chinese character"</p>
* <p>can't end with "_"</p>
* <p>length is between 6 to 20</p>
*/
public static final String REGEX_USERNAME = "^[\\w\\u4e00-\\u9fa5]{6,20}(?<!_)$";
/**
* Regex of date which pattern is "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)$";
/**
* Regex of ip address.
*/
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?)"; ///////////////////////////////////////////////////////////////////////////
// The following come from http://tool.oschina.net/regex
/////////////////////////////////////////////////////////////////////////// /**
* Regex of double-byte characters.
*/
public static final String REGEX_DOUBLE_BYTE_CHAR = "[^\\x00-\\xff]";
/**
* Regex of blank line.
*/
public static final String REGEX_BLANK_LINE = "\\n\\s*\\r";
/**
* Regex of QQ number.
*/
public static final String REGEX_QQ_NUM = "[1-9][0-9]{4,}";
/**
* Regex of postal code in China.
*/
public static final String REGEX_CHINA_POSTAL_CODE = "[1-9]\\d{5}(?!\\d)";
/**
* Regex of positive integer.
*/
public static final String REGEX_POSITIVE_INTEGER = "^[1-9]\\d*$";
/**
* Regex of negative integer.
*/
public static final String REGEX_NEGATIVE_INTEGER = "^-[1-9]\\d*$";
/**
* Regex of integer.
*/
public static final String REGEX_INTEGER = "^-?[1-9]\\d*$";
/**
* Regex of non-negative integer.
*/
public static final String REGEX_NOT_NEGATIVE_INTEGER = "^[1-9]\\d*|0$";
/**
* Regex of non-positive integer.
*/
public static final String REGEX_NOT_POSITIVE_INTEGER = "^-[1-9]\\d*|0$";
/**
* Regex of positive float.
*/
public static final String REGEX_POSITIVE_FLOAT = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$";
/**
* Regex of negative float.
*/
public static final String REGEX_NEGATIVE_FLOAT = "^-[1-9]\\d*\\.\\d*|-0\\.\\d*[1-9]\\d*$"; private RegexUtil() {
throw new UnsupportedOperationException("u can't instantiate me...");
} ///////////////////////////////////////////////////////////////////////////
// If u want more please visit http://toutiao.com/i6231678548520731137
/////////////////////////////////////////////////////////////////////////// /**
* Return whether input matches regex of simple mobile.
*
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isMobileSimple(final CharSequence input) {
return isMatch(REGEX_MOBILE_SIMPLE, input);
} /**
* Return whether input matches regex of exact mobile.
*
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isMobileExact(final CharSequence input) {
return isMatch(REGEX_MOBILE_EXACT, input);
} /**
* Return whether input matches regex of telephone number.
*
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isTel(final CharSequence input) {
return isMatch(REGEX_TEL, input);
} /**
* Return whether input matches regex of id card number which length is 15.
*
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isIDCard15(final CharSequence input) {
return isMatch(REGEX_ID_CARD15, input);
} /**
* Return whether input matches regex of id card number which length is 18.
*
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isIDCard18(final CharSequence input) {
return isMatch(REGEX_ID_CARD18, input);
} /**
* Return whether input matches regex of email.
*
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isEmail(final CharSequence input) {
return isMatch(REGEX_EMAIL, input);
} /**
* Return whether input matches regex of url.
*
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isURL(final CharSequence input) {
return isMatch(REGEX_URL, input);
} /**
* Return whether input matches regex of Chinese character.
*
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isZh(final CharSequence input) {
return isMatch(REGEX_ZH, input);
} /**
* Return whether input matches regex of username.
* <p>scope for "a-z", "A-Z", "0-9", "_", "Chinese character"</p>
* <p>can't end with "_"</p>
* <p>length is between 6 to 20</p>.
*
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isUsername(final CharSequence input) {
return isMatch(REGEX_USERNAME, input);
} /**
* Return whether input matches regex of date which pattern is "yyyy-MM-dd".
*
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isDate(final CharSequence input) {
return isMatch(REGEX_DATE, input);
} /**
* Return whether input matches regex of ip address.
*
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isIP(final CharSequence input) {
return isMatch(REGEX_IP, input);
} /**
* Return whether input matches the regex.
*
* @param regex The regex.
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isMatch(final String regex, final CharSequence input) {
return input != null && input.length() > 0 && Pattern.matches(regex, input);
} /**
* Return the list of input matches the regex.
*
* @param regex The regex.
* @param input The input.
* @return the list of input matches the regex
*/
public static List<String> getMatches(final String regex, final CharSequence input) {
if (input == null)
return Collections.emptyList();
List<String> matches = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
matches.add(matcher.group());
}
return matches;
} /**
* Splits input around matches of the regex.
*
* @param input The input.
* @param regex The regex.
* @return the array of strings computed by splitting input around matches of regex
*/
public static String[] getSplits(final String input, final String regex) {
if (input == null)
return new String[0];
return input.split(regex);
} /**
* Replace the first subsequence of the input sequence that matches the
* regex with the given replacement string.
*
* @param input The input.
* @param regex The regex.
* @param replacement The replacement string.
* @return the string constructed by replacing the first matching
* subsequence by the replacement string, substituting captured
* subsequences as needed
*/
public static String getReplaceFirst(final String input,
final String regex,
final String replacement) {
if (input == null)
return "";
return Pattern.compile(regex).matcher(input).replaceFirst(replacement);
} /**
* Replace every subsequence of the input sequence that matches the
* pattern with the given replacement string.
*
* @param input The input.
* @param regex The regex.
* @param replacement The replacement string.
* @return the string constructed by replacing each matching subsequence
* by the replacement string, substituting captured subsequences
* as needed
*/
public static String getReplaceAll(final String input,
final String regex,
final String replacement) {
if (input == null)
return "";
return Pattern.compile(regex).matcher(input).replaceAll(replacement);
}
}

RegexUtil的更多相关文章

  1. Java正则表达式实现字符串的动态多替换

    需求场景: 今天在处理SQL语句的时候,由于数据库中存的格式是VARCHAR2型的,这就需要对SQL语句中WHERE条件后边的带数字的字符串加上单引号,对于字符串的处理,首先想到的就是正则表达式,对正 ...

  2. Eclipse常用开发插件

    以下是我整理的自己开发过程中的常用Eclipse插件,按字母排序: (1)    AmaterasUML         介绍:Eclipse的UML插件,支持UML活动图,class图,sequen ...

  3. java最全的验证类封装

    package com.tongrong.utils; import java.util.Collection; import java.util.Map; import java.util.rege ...

  4. 常用Eclipse插件在线安装地址

    Srping IDE http://www.springsource.com/update/e3.5   EasyShellhttp://pluginbox.sourceforge.net   M2E ...

  5. 转:Eclipse常用开发插件

    以下是我整理的自己开发过程中的常用Eclipse插件,按字母排序: (1)    AmaterasUML         介绍:Eclipse的UML插件,支持UML活动图,class图,sequen ...

  6. Eclipse常用快捷键windows

    Ctrl+1:快速修正Ctrl+W: 关闭当前文件ctrl+O:打开outlineCtrl+D: 删除当前行 Ctrl+L: 定位在某行Ctrl+Q:转到上次修改位置Ctrl+/:注释代码Ctrl+H ...

  7. 史上最全Java表单验证封装类

    package com.tongrong.utils; import java.util.Collection; import java.util.Map; import java.util.rege ...

  8. Eclipse常用的插件安装

    嫌公司用的eclipse不爽,准备自己弄一个,diy的,没想到装插得烦死人. 诱惑人的“常用插件”: (1)    AmaterasUML        介绍:Eclipse的UML插件,支持UML活 ...

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

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

随机推荐

  1. Linux(CENTOS7) YUM方式安装mysql5.7

    参考地址:https://www.cnblogs.com/linjiqin/p/7611204.html 注:该地址标题写的是CENTOS6.*版本的,但是我在我的CENTOS7.*上面安装是完美进行 ...

  2. git 一些操作

    1. 代码相关 克隆代码 git clone xxx.git 拉取代码 git pull 查看 修改的 状态 git status 推送代码 git push add 或者 修改代码之后 回滚到 未修 ...

  3. 一个算法题--Self Crossing

    You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to th ...

  4. JDBC,ResultSet对像多次使用后再关闭的问题

    原文链接:https://yq.aliyun.com/wenzhang/show_111763 问题描述 //代码... ResultSet rs = this.conn.prepareStateme ...

  5. Codeforces Round #619 (Div. 2)E思维+二维RMQ

    题:https://codeforces.com/contest/1301/problem/E 题意:给个n*m的图形,q个询问,每次询问问询问区间最大的合法logo的面积是多少 分析:由于logo是 ...

  6. 2020/1/27代码审计学习之SQL注入漏洞

    PHP代码审计SQL注入漏洞 0x00 首先明确什么是SQL注入,SQL语句必须掌握. 常见的注入总的来说可以分为两大类:数字型和字符型. 这两类中包含了诸如报错注入,宽字节注入,盲注,二次注入,co ...

  7. AttributeError: module 'selenium.webdriver.common.service' has no attribute 'Service'

    今天爬虫时需要使用到selenium, 使用pip install selenium进行安装. 可是一开始写程序就遇到了AttributeError: module 'selenium.webdriv ...

  8. Scala(一)——scala+Idea环境配置

    Java虚拟机的确是很强大,有很多计算机语言可以运行在虚拟机上,完善了虚拟机上多语言编程. 近年来,大数据云计算,大数据的火爆也让一些小众语言火了起来,如Python,Scala等.这些语言编写简单, ...

  9. JavaScript 闭包究竟是什么JavaScript 闭包究竟是什么

    用JavaScript一年多了,闭包总是让人二丈和尚摸不着头脑.陆陆续续接触了一些闭包的知识,也犯过几次因为不理解闭包导致的错误,一年多了资料也看了一些,但还是不是非常明白,最近偶然看了一下 jQue ...

  10. mysql安装完之后,登陆后发现只有两个数据库

    mysql安装完之后,登陆后发现只有两个数据库:mysql> show databases;+--------------------+| Database           |+------ ...