添加重载方法,允许正则表达式以字符串的形式作为参数,并对字符串对应的正则表达式进行缓存。

feature/net-util
ZhouXY108 2023-04-15 03:55:21 +08:00
parent de73ce4184
commit 4d6e116fc5
1 changed files with 49 additions and 5 deletions

View File

@ -16,17 +16,39 @@
package xyz.zhouxy.plusone.commons.util;
import javax.annotation.Nonnull;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
public class RegexUtil {
public static boolean matches(CharSequence input, Pattern regex) {
private static final Map<String, Pattern> PATTERN_CACHE = new ConcurrentHashMap<>();
public static Pattern getPattern(final String regex) {
Objects.requireNonNull(regex);
Pattern pattern;
if (!PATTERN_CACHE.containsKey(regex)) {
pattern = Pattern.compile(regex);
PATTERN_CACHE.put(regex, pattern);
} else {
pattern = PATTERN_CACHE.get(regex);
}
return pattern;
}
public static boolean matches(@Nonnull CharSequence input, @Nonnull String regex) {
return matches(input, getPattern(regex));
}
public static boolean matches(@Nonnull CharSequence input, @Nonnull Pattern regex) {
return regex.matcher(input).matches();
}
public static boolean matchesOr(CharSequence input, Pattern... regexs) {
public static boolean matchesOr(@Nonnull CharSequence input, String... regexes) {
boolean isMatched;
for (Pattern regex : regexs) {
for (String regex : regexes) {
isMatched = matches(input, regex);
if (isMatched) {
return true;
@ -35,9 +57,20 @@ public class RegexUtil {
return false;
}
public static boolean matchesAnd(CharSequence input, Pattern... regexs) {
public static boolean matchesOr(@Nonnull CharSequence input, Pattern... patterns) {
boolean isMatched;
for (Pattern regex : regexs) {
for (Pattern pattern : patterns) {
isMatched = matches(input, pattern);
if (isMatched) {
return true;
}
}
return false;
}
public static boolean matchesAnd(@Nonnull CharSequence input, String... regexes) {
boolean isMatched;
for (String regex : regexes) {
isMatched = matches(input, regex);
if (!isMatched) {
return false;
@ -46,6 +79,17 @@ public class RegexUtil {
return true;
}
public static boolean matchesAnd(@Nonnull CharSequence input, Pattern... patterns) {
boolean isMatched;
for (Pattern pattern : patterns) {
isMatched = matches(input, pattern);
if (!isMatched) {
return false;
}
}
return true;
}
private RegexUtil() {
throw new IllegalStateException("Utility class");
}