类 RegexUtils
java.lang.Object
spring.turbo.util.RegexUtils
正则表达式相关工具
- 从以下版本开始:
- 1.0.6
- 作者:
- 应卓
- 另请参阅:
-
方法概要
修饰符和类型方法说明static booleanisValidRegex(String regex) 判断正则表达式是否合法static String移除所有的匹配的子字符串,这个方法默认不使用Pattern.DOTALL模式static String移除所有的匹配的子字符串static StringremoveFirst(String text, String regex) 移除第一个匹配的子字符串,这个方法默认不使用Pattern.DOTALL模式static StringremoveFirst(String text, Pattern regex) 移除第一个匹配的子字符串static StringremovePattern(String text, String regex) 移除所有匹配的子字符串,这个方法默认使用Pattern.DOTALL模式static StringreplaceAll(String text, String regex, String replacement) 替换所有的匹配的子字符串,这个方法默认不使用Pattern.DOTALL模式static StringreplaceAll(String text, Pattern regex, String replacement) 替换所有的匹配的子字符串static StringreplaceFirst(String text, String regex, String replacement) 替换第一个匹配的字串,这个方法不使用Pattern.DOTALL模式static StringreplaceFirst(String text, Pattern regex, String replacement) 替换第一个匹配的子字符串static StringreplacePattern(String text, String regex, String replacement) 替换所有的匹配的字串,这个方法使用Pattern.DOTALL模式
-
方法详细资料
-
removeAll
移除所有的匹配的子字符串
RegexUtils.removeAll("any", Pattern.compile("")) = "any" RegexUtils.removeAll("any", Pattern.compile(".*")) = "" RegexUtils.removeAll("any", Pattern.compile(".+")) = "" RegexUtils.removeAll("abc", Pattern.compile(".?")) = "" RegexUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>")) = "A\nB" RegexUtils.removeAll("A<__>\n<__>B", Pattern.compile("(?s)<.*>")) = "AB" RegexUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>", Pattern.DOTALL)) = "AB" RegexUtils.removeAll("ABCabc123abc", Pattern.compile("[a-z]")) = "ABC123"- 参数:
text- 原始字符串regex- 正则表达式- 返回:
- 结果
- 另请参阅:
-
removeAll
移除所有的匹配的子字符串,这个方法默认不使用
Pattern.DOTALL模式RegexUtils.removeAll("any", "") = "any" RegexUtils.removeAll("any", ".*") = "" RegexUtils.removeAll("any", ".+") = "" RegexUtils.removeAll("abc", ".?") = "" RegexUtils.removeAll("A<__>\n<__>B", "<.*>") = "A\nB" RegexUtils.removeAll("A<__>\n<__>B", "(?s)<.*>") = "AB" RegexUtils.removeAll("ABCabc123abc", "[a-z]") = "ABC123"- 参数:
text- 原始字符串regex- 正则表达式- 返回:
- 结果
- 抛出:
PatternSyntaxException- 正则表达式非法- 另请参阅:
-
removeFirst
移除第一个匹配的子字符串
RegexUtils.removeFirst("any", Pattern.compile("")) = "any" RegexUtils.removeFirst("any", Pattern.compile(".*")) = "" RegexUtils.removeFirst("any", Pattern.compile(".+")) = "" RegexUtils.removeFirst("abc", Pattern.compile(".?")) = "bc" RegexUtils.removeFirst("A<__>\n<__>B", Pattern.compile("<.*>")) = "A\n<__>B" RegexUtils.removeFirst("A<__>\n<__>B", Pattern.compile("(?s)<.*>")) = "AB" RegexUtils.removeFirst("ABCabc123", Pattern.compile("[a-z]")) = "ABCbc123" RegexUtils.removeFirst("ABCabc123abc", Pattern.compile("[a-z]+")) = "ABC123abc"- 参数:
text- 原始字符串regex- 正则表达式- 返回:
- 结果
- 另请参阅:
-
removeFirst
移除第一个匹配的子字符串,这个方法默认不使用
Pattern.DOTALL模式RegexUtils.removeFirst("any", "") = "any" RegexUtils.removeFirst("any", ".*") = "" RegexUtils.removeFirst("any", ".+") = "" RegexUtils.removeFirst("abc", ".?") = "bc" RegexUtils.removeFirst("A<__>\n<__>B", "<.*>") = "A\n<__>B" RegexUtils.removeFirst("A<__>\n<__>B", "(?s)<.*>") = "AB" RegexUtils.removeFirst("ABCabc123", "[a-z]") = "ABCbc123" RegexUtils.removeFirst("ABCabc123abc", "[a-z]+") = "ABC123abc"- 参数:
text- 原始字符串regex- 正则表达式- 返回:
- 结果
- 抛出:
PatternSyntaxException- 正则表达式非法- 另请参阅:
-
removePattern
移除所有匹配的子字符串,这个方法默认使用
Pattern.DOTALL模式RegexUtils.removePattern("A<__>\n<__>B", "<.*>") = "AB" RegexUtils.removePattern("ABCabc123", "[a-z]") = "ABC123"- 参数:
text- 原始字符串regex- 正则表达式- 返回:
- 结果
- 抛出:
PatternSyntaxException- 正则表达式非法- 另请参阅:
-
replaceAll
@NonNull public static String replaceAll(@NonNull String text, @NonNull Pattern regex, @NonNull String replacement) 替换所有的匹配的子字符串
RegexUtils.replaceAll("", Pattern.compile(""), "zzz") = "zzz" RegexUtils.replaceAll("", Pattern.compile(".*"), "zzz") = "zzz" RegexUtils.replaceAll("", Pattern.compile(".+"), "zzz") = "" RegexUtils.replaceAll("abc", Pattern.compile(""), "ZZ") = "ZZaZZbZZcZZ" RegexUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>"), "z") = "z\nz" RegexUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>", Pattern.DOTALL), "z") = "z" RegexUtils.replaceAll("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z") = "z" RegexUtils.replaceAll("ABCabc123", Pattern.compile("[a-z]"), "_") = "ABC___123" RegexUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "_") = "ABC_123" RegexUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "") = "ABC123" RegexUtils.replaceAll("Lorem ipsum dolor sit", Pattern.compile("( +)([a-z]+)"), "_$2") = "Lorem_ipsum_dolor_sit"- 参数:
text- 原始字符串regex- 正则表达式replacement- 替换字符串- 返回:
- 结果
- 另请参阅:
-
replaceAll
@NonNull public static String replaceAll(@NonNull String text, @NonNull String regex, @NonNull String replacement) 替换所有的匹配的子字符串,这个方法默认不使用
Pattern.DOTALL模式RegexUtils.replaceAll("", "", "zzz") = "zzz" RegexUtils.replaceAll("", ".*", "zzz") = "zzz" RegexUtils.replaceAll("", ".+", "zzz") = "" RegexUtils.replaceAll("abc", "", "ZZ") = "ZZaZZbZZcZZ" RegexUtils.replaceAll("<__>\n<__>", "<.*>", "z") = "z\nz" RegexUtils.replaceAll("<__>\n<__>", "(?s)<.*>", "z") = "z" RegexUtils.replaceAll("ABCabc123", "[a-z]", "_") = "ABC___123" RegexUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "_") = "ABC_123" RegexUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "") = "ABC123" RegexUtils.replaceAll("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2") = "Lorem_ipsum_dolor_sit"- 参数:
text- 原始字符串regex- 正则表达式replacement- 替换字符串- 返回:
- 结果
- 抛出:
PatternSyntaxException- 正则表达式非法- 另请参阅:
-
replaceFirst
@NonNull public static String replaceFirst(@NonNull String text, @NonNull Pattern regex, @NonNull String replacement) 替换第一个匹配的子字符串
RegexUtils.replaceFirst("", Pattern.compile(""), "zzz") = "zzz" RegexUtils.replaceFirst("", Pattern.compile(".*"), "zzz") = "zzz" RegexUtils.replaceFirst("", Pattern.compile(".+"), "zzz") = "" RegexUtils.replaceFirst("abc", Pattern.compile(""), "ZZ") = "ZZabc" RegexUtils.replaceFirst("<__>\n<__>", Pattern.compile("<.*>"), "z") = "z\n<__>" RegexUtils.replaceFirst("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z") = "z" RegexUtils.replaceFirst("ABCabc123", Pattern.compile("[a-z]"), "_") = "ABC_bc123" RegexUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "_") = "ABC_123abc" RegexUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "") = "ABC123abc" RegexUtils.replaceFirst("Lorem ipsum dolor sit", Pattern.compile("( +)([a-z]+)"), "_$2") = "Lorem_ipsum dolor sit"- 参数:
text- 原始字符串regex- 正则表达式replacement- 替换字符串- 返回:
- 结果
- 另请参阅:
-
replaceFirst
@NonNull public static String replaceFirst(@NonNull String text, @NonNull String regex, @NonNull String replacement) 替换第一个匹配的字串,这个方法不使用
Pattern.DOTALL模式RegexUtils.replaceFirst("", "", "zzz") = "zzz" RegexUtils.replaceFirst("", ".*", "zzz") = "zzz" RegexUtils.replaceFirst("", ".+", "zzz") = "" RegexUtils.replaceFirst("abc", "", "ZZ") = "ZZabc" RegexUtils.replaceFirst("<__>\n<__>", "<.*>", "z") = "z\n<__>" RegexUtils.replaceFirst("<__>\n<__>", "(?s)<.*>", "z") = "z" RegexUtils.replaceFirst("ABCabc123", "[a-z]", "_") = "ABC_bc123" RegexUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "_") = "ABC_123abc" RegexUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "") = "ABC123abc" RegexUtils.replaceFirst("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2") = "Lorem_ipsum dolor sit"- 参数:
text- 原始字符串regex- 正则表达式replacement- 替换字符串- 返回:
- 结果
- 抛出:
PatternSyntaxException- 正则表达式非法- 另请参阅:
-
replacePattern
@NonNull public static String replacePattern(@NonNull String text, @NonNull String regex, @NonNull String replacement) 替换所有的匹配的字串,这个方法使用
Pattern.DOTALL模式RegexUtils.replacePattern("", "", "zzz") = "zzz" RegexUtils.replacePattern("", ".*", "zzz") = "zzz" RegexUtils.replacePattern("", ".+", "zzz") = "" RegexUtils.replacePattern("<__>\n<__>", "<.*>", "z") = "z" RegexUtils.replacePattern("ABCabc123", "[a-z]", "_") = "ABC___123" RegexUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "_") = "ABC_123" RegexUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "") = "ABC123" RegexUtils.replacePattern("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2") = "Lorem_ipsum_dolor_sit"- 参数:
text- 原始字符串regex- 正则表达式replacement- 替换的字符串- 返回:
- 结果
- 抛出:
PatternSyntaxException- 正则表达式非法- 另请参阅:
-
isValidRegex
判断正则表达式是否合法- 参数:
regex- 正则表达式- 返回:
- 合法时返回
true否则返回false
-