一、代码
package com.xxxx.xclouddesk.utils;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import java.util.ArrayList;
import java.util.List;
/**
* @author nick
*/
@Slf4j
public class RegularUtils {
/**
* 判断一个字符串是否符合正则格式 举例: regExp=你好(中国|世界) text=你好世界或者你好中国 返回 true
* @param text 文本
* @param regExp 正则表达式
* @return
*/
public static boolean isRegular(String text,String regExp){
if (StringUtils.isBlank(text) || StringUtils.isBlank(regExp)) {
return false;
}
return java.util.regex.Pattern.matches(regExp, text);
}
/**
* 校验字符串是否是 json 格式
* @param json 文本
* @return
*/
public static boolean isJson(String json) {
if (StringUtils.isBlank(json)) {
return false;
}
try {
new JsonParser().parse(json);
return true;
} catch (JsonParseException e) {
return false;
}
}
/**
* 自定义正则
* @param text 文本
* @param regExp 正则表达式
* @param regularSet 自定义正则表达式
* @return
*/
public static boolean isExcluded(String text, String regExp, List<Pattern> regularSet){
if (StringUtils.isBlank(text) || StringUtils.isBlank(regExp)) {
return false;
}
return false;
}
/**
* LGF正则 举例 (今天|明天|后天)北京[的]天气[预报] true 今天北京的天气预报 false 前天北京的天气预报
* @param text 匹配的源字符串
* @param regExp 匹配的正规表达式
* @return 如果源字符串符合要求返回真,否则返回假
*/
public static boolean isHardRegexpValidate(String text, String regExp) {
if (StringUtils.isBlank(text) || StringUtils.isBlank(regExp)) {
return false;
}
try {
// 用于定义正规表达式对象模板类型
PatternCompiler compiler = new Perl5Compiler();
// 正规表达式比较批配对象
PatternMatcher matcher = new Perl5Matcher();
// 实例大小大小写敏感的正规表达式模板
Pattern hardPattern = compiler.compile(regExp);
// 返回批配结果
return matcher.contains(text, hardPattern);
} catch (MalformedPatternException e) {
log.info("解析异常:{}",e.getMessage());
}
return false;
}
/**
* 判断是否匹配正则
*
* @param regex 正则表达式
* @param input 要匹配的字符串
* @return {@code true}: 匹配
* {@code false}: 不匹配
*/
public static boolean isMatch(String regex, CharSequence input) {
return input != null && input.length() > 0 && java.util.regex.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<>();
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regex);
java.util.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 java.util.regex.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 java.util.regex.Pattern.compile(regex).matcher(input).replaceAll(replacement);
}
// public static void main(String[] args) {
// System.out.println(isHardRegexpValidate("今天北京的天气预报","(今天|明天|后天)北京[的]天气[预报]"));
// System.out.println(isRegular("你好中国","你好(中国|世界)"));
// }
}
二、依赖
<dependency>
<groupId>oro</groupId>
<artifactId>oro</artifactId>
<version>2.0.7</version>
</dependency>