/** * <p>Removes each substring of the source String that matches the given regular expression using the DOTALL option.</p> * * This call is a {@code null} safe equivalent to: * <ul> * <li>{@code text.replaceAll("(?s)" + regex, StringUtils.EMPTY)}</li> * <li>{@code Pattern.compile(regex, Pattern.DOTALL).matcher(text).replaceAll(StringUtils.EMPTY)}</li> * </ul> * * <p>A {@code null} reference passed to this method is a no-op.</p> * * <pre> * StringUtils.removePattern(null, *) = null * StringUtils.removePattern("any", (String) null) = "any" * StringUtils.removePattern("A<__>\n<__>B", "<.*>") = "AB" * StringUtils.removePattern("ABCabc123", "[a-z]") = "ABC123" * </pre> * * @param text * the source string * @param regex * the regular expression to which this string is to be matched * @return The resulting {@code String} * @see #replacePattern(String, String, String) * @see String#replaceAll(String, String) * @see Pattern#DOTALL */ public static String removePattern(final String text, final String regex) { return replacePattern(text, regex, StringUtils.EMPTY); }
return RegExUtils.replacePattern(source, regex, replacement);
@Test public void testReplacePattern_StringStringString() { assertNull(RegExUtils.replacePattern(null, "", "")); assertEquals("any", RegExUtils.replacePattern("any", (String) null, "")); assertEquals("any", RegExUtils.replacePattern("any", "", null)); assertEquals("zzz", RegExUtils.replacePattern("", "", "zzz")); assertEquals("zzz", RegExUtils.replacePattern("", ".*", "zzz")); assertEquals("", RegExUtils.replacePattern("", ".+", "zzz")); assertEquals("z", RegExUtils.replacePattern("<__>\n<__>", "<.*>", "z")); assertEquals("z", RegExUtils.replacePattern("<__>\\n<__>", "<.*>", "z")); assertEquals("X", RegExUtils.replacePattern("<A>\nxy\n</A>", "<A>.*</A>", "X")); assertEquals("ABC___123", RegExUtils.replacePattern("ABCabc123", "[a-z]", "_")); assertEquals("ABC_123", RegExUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "_")); assertEquals("ABC123", RegExUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "")); assertEquals("Lorem_ipsum_dolor_sit", RegExUtils.replacePattern("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2")); }
/** * <p>Removes each substring of the source String that matches the given regular expression using the DOTALL option.</p> * * This call is a {@code null} safe equivalent to: * <ul> * <li>{@code text.replaceAll("(?s)" + regex, StringUtils.EMPTY)}</li> * <li>{@code Pattern.compile(regex, Pattern.DOTALL).matcher(text).replaceAll(StringUtils.EMPTY)}</li> * </ul> * * <p>A {@code null} reference passed to this method is a no-op.</p> * * <pre> * StringUtils.removePattern(null, *) = null * StringUtils.removePattern("any", (String) null) = "any" * StringUtils.removePattern("A<__>\n<__>B", "<.*>") = "AB" * StringUtils.removePattern("ABCabc123", "[a-z]") = "ABC123" * </pre> * * @param text * the source string * @param regex * the regular expression to which this string is to be matched * @return The resulting {@code String} * @see #replacePattern(String, String, String) * @see String#replaceAll(String, String) * @see Pattern#DOTALL */ public static String removePattern(final String text, final String regex) { return replacePattern(text, regex, StringUtils.EMPTY); }
/** * <p>Removes each substring of the source String that matches the given regular expression using the DOTALL option.</p> * * This call is a {@code null} safe equivalent to: * <ul> * <li>{@code text.replaceAll("(?s)" + regex, StringUtils.EMPTY)}</li> * <li>{@code Pattern.compile(regex, Pattern.DOTALL).matcher(text).replaceAll(StringUtils.EMPTY)}</li> * </ul> * * <p>A {@code null} reference passed to this method is a no-op.</p> * * <pre> * StringUtils.removePattern(null, *) = null * StringUtils.removePattern("any", (String) null) = "any" * StringUtils.removePattern("A<__>\n<__>B", "<.*>") = "AB" * StringUtils.removePattern("ABCabc123", "[a-z]") = "ABC123" * </pre> * * @param text * the source string * @param regex * the regular expression to which this string is to be matched * @return The resulting {@code String} * @see #replacePattern(String, String, String) * @see String#replaceAll(String, String) * @see Pattern#DOTALL */ public static String removePattern(final String text, final String regex) { return replacePattern(text, regex, StringUtils.EMPTY); }
/** * <p>Removes each substring of the source String that matches the given regular expression using the DOTALL option.</p> * * This call is a {@code null} safe equivalent to: * <ul> * <li>{@code text.replaceAll("(?s)" + regex, StringUtils.EMPTY)}</li> * <li>{@code Pattern.compile(regex, Pattern.DOTALL).matcher(text).replaceAll(StringUtils.EMPTY)}</li> * </ul> * * <p>A {@code null} reference passed to this method is a no-op.</p> * * <pre> * StringUtils.removePattern(null, *) = null * StringUtils.removePattern("any", (String) null) = "any" * StringUtils.removePattern("A<__>\n<__>B", "<.*>") = "AB" * StringUtils.removePattern("ABCabc123", "[a-z]") = "ABC123" * </pre> * * @param text * the source string * @param regex * the regular expression to which this string is to be matched * @return The resulting {@code String} * @see #replacePattern(String, String, String) * @see String#replaceAll(String, String) * @see Pattern#DOTALL */ public static String removePattern(final String text, final String regex) { return replacePattern(text, regex, StringUtils.EMPTY); }
return RegExUtils.replacePattern(source, regex, replacement);
return RegExUtils.replacePattern(source, regex, replacement);
return RegExUtils.replacePattern(source, regex, replacement);
private static String cleanUpDescription(final String propDescription) { var description = propDescription; val format = "<code>%s</code>"; if (StringUtils.isNotBlank(description)) { var matcher = PATTERN_DESCRIPTION_CODE.matcher(description); if (matcher.find()) { description = RegExUtils.replacePattern(description, PATTERN_DESCRIPTION_CODE.pattern(), String.format(format, matcher.group(1))); } matcher = PATTERN_DESCRIPTION_LINK.matcher(description); if (matcher.find()) { val replacement = "See ".concat(String.format(format, matcher.group(1))); description = RegExUtils.replacePattern(description, PATTERN_DESCRIPTION_LINK.pattern(), replacement); } matcher = PATTERN_DESCRIPTION_SEE.matcher(description); if (matcher.find()) { val replacement = "See ".concat(String.format(format, matcher.group(1))); description = RegExUtils.replacePattern(description, PATTERN_DESCRIPTION_SEE.pattern(), replacement); } return description; } return propDescription; }