| 限定符和类型 | 方法和说明 |
|---|---|
static boolean |
isValidRegex(String regex)
判断正则表达式是否合法
|
static String |
removeAll(String text,
Pattern regex)
移除所有的匹配的子字符串
RegexUtils.removeAll("any", Pattern.compile("")) = "any"
RegexUtils.removeAll("any", Pattern.compile(".*")) = ""
RegexUtils.removeAll("any", Pattern.compile(".+")) = ""
RegexUtils.removeAll("abc", Pattern.compile(".?"))
|
static String |
removeAll(String text,
String regex)
移除所有的匹配的子字符串,这个方法默认不使用
Pattern.DOTALL 模式
RegexUtils.removeAll("any", "") = "any"
RegexUtils.removeAll("any", ".*") = ""
RegexUtils.removeAll("any", ".+") = ""
RegexUtils.removeAll("abc", ".?") |
static String |
removeFirst(String text,
Pattern regex)
移除第一个匹配的子字符串
RegexUtils.removeFirst("any", Pattern.compile("")) = "any"
RegexUtils.removeFirst("any", Pattern.compile(".*")) = ""
RegexUtils.removeFirst("any", Pattern.compile(".+")) = ""
RegexUtils.removeFirst("abc", Pattern.compile(".?"))
|
static String |
removeFirst(String text,
String regex)
移除第一个匹配的子字符串,这个方法默认不使用
Pattern.DOTALL 模式
RegexUtils.removeFirst("any", "") = "any"
RegexUtils.removeFirst("any", ".*") = ""
RegexUtils.removeFirst("any", ".+") = ""
RegexUtils.removeFirst("abc", ".?") |
static String |
removePattern(String text,
String regex)
移除所有匹配的子字符串,这个方法默认使用
Pattern.DOTALL 模式
RegexUtils.removePattern("A<__>\n<__>B", "<. |
static String |
replaceAll(String text,
Pattern regex,
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("<.
|
static String |
replaceAll(String text,
String regex,
String replacement)
替换所有的匹配的子字符串,这个方法默认不使用
Pattern.DOTALL 模式
RegexUtils.replaceAll("", "", "zzz") = "zzz"
RegexUtils.replaceAll("", ".*", "zzz") = "zzz"
RegexUtils.replaceAll("", ".+", "zzz") = ""
RegexUtils.replaceAll("abc", "", "ZZ") = "ZZaZZbZZcZZ"
RegexUtils.replaceAll("<__>\n<__>", "<. |
static String |
replaceFirst(String text,
Pattern regex,
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("<.
|
static String |
replaceFirst(String text,
String regex,
String replacement)
替换第一个匹配的字串,这个方法不使用
Pattern.DOTALL 模式
RegexUtils.replaceFirst("", "", "zzz") = "zzz"
RegexUtils.replaceFirst("", ".*", "zzz") = "zzz"
RegexUtils.replaceFirst("", ".+", "zzz") = ""
RegexUtils.replaceFirst("abc", "", "ZZ") = "ZZabc"
RegexUtils.replaceFirst("<__>\n<__>", "<. |
static String |
replacePattern(String text,
String regex,
String replacement)
替换所有的匹配的字串,这个方法使用
Pattern.DOTALL 模式
RegexUtils.replacePattern("", "", "zzz") = "zzz"
RegexUtils.replacePattern("", ".*", "zzz") = "zzz"
RegexUtils.replacePattern("", ".+", "zzz") = ""
RegexUtils.replacePattern("<__>\n<__>", "<. |
@NonNull public static String removeAll(@NonNull String text, @NonNull Pattern regex)
移除所有的匹配的子字符串
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 - 正则表达式replaceAll(String, Pattern, String),
Matcher.replaceAll(String),
Pattern@NonNull public static String removeAll(@NonNull String text, @NonNull String regex)
移除所有的匹配的子字符串,这个方法默认不使用 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 - 正则表达式非法replaceAll(String, String, String),
removePattern(String, String),
String.replaceAll(String, String),
Pattern,
Pattern.DOTALL@NonNull public static String removeFirst(@NonNull String text, @NonNull Pattern regex)
移除第一个匹配的子字符串
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 - 正则表达式replaceFirst(String, Pattern, String),
Matcher.replaceFirst(String),
Pattern@NonNull public static String removeFirst(@NonNull String text, @NonNull String regex)
移除第一个匹配的子字符串,这个方法默认不使用 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 - 正则表达式非法replaceFirst(String, String, String),
String.replaceFirst(String, String),
Pattern,
Pattern.DOTALL@NonNull public static String removePattern(@NonNull String text, @NonNull String regex)
移除所有匹配的子字符串,这个方法默认使用 Pattern.DOTALL 模式
RegexUtils.removePattern("A<__>\n<__>B", "<.*>") = "AB"
RegexUtils.removePattern("ABCabc123", "[a-z]") = "ABC123"
text - 原始字符串regex - 正则表达式PatternSyntaxException - 正则表达式非法replacePattern(String, String, String),
String.replaceAll(String, String),
Pattern.DOTALL@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 - 替换字符串Matcher.replaceAll(String),
Pattern@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 - 正则表达式非法replacePattern(String, String, String),
String.replaceAll(String, String),
Pattern,
Pattern.DOTALL@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 - 替换字符串Matcher.replaceFirst(String),
Pattern@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 - 正则表达式非法String.replaceFirst(String, String),
Pattern,
Pattern.DOTALL@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 - 正则表达式非法replaceAll(String, String, String),
String.replaceAll(String, String),
Pattern.DOTALLpublic static boolean isValidRegex(@Nullable
String regex)
regex - 正则表达式true 否则返回 falseCopyright © 2022. All rights reserved.