Skip to content

Commit e7d001f

Browse files
committed
Add RegExUtils.removePattern(CharSequence, String) and deprecate
RegExUtils.removePattern(String, String)
1 parent 6e0c4f9 commit e7d001f

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ The <action> type attribute can be add,update,fix,remove.
9191
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Predicates.</action>
9292
<action type="add" dev="ggregory" due-to="Gary Gregory">Add RegExUtils.dotAllMatcher(String, CharSequence) and deprecate RegExUtils.dotAllMatcher(String, String).</action>
9393
<action type="add" dev="ggregory" due-to="Gary Gregory">Add RegExUtils.replacePattern(CharSequence, String, String) and deprecate RegExUtils.replacePattern(String, String, String).</action>
94+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add RegExUtils.removePattern(CharSequence, String) and deprecate RegExUtils.removePattern(String, String).</action>
9495
<!-- UPDATE -->
9596
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 79 #1267, #1277, #1283, #1288, #1302.</action>
9697
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">[site] Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 #1300.</action>

src/main/java/org/apache/commons/lang3/RegExUtils.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static String removeAll(final String text, final Pattern regex) {
113113
*
114114
* <p>A {@code null} reference passed to this method is a no-op.</p>
115115
*
116-
* <p>Unlike in the {@link #removePattern(String, String)} method, the {@link Pattern#DOTALL} option
116+
* <p>Unlike in the {@link #removePattern(CharSequence, String)} method, the {@link Pattern#DOTALL} option
117117
* is NOT automatically added.
118118
* To use the DOTALL option prepend {@code "(?s)"} to the regex.
119119
* DOTALL is also known as single-line mode in Perl.</p>
@@ -139,7 +139,7 @@ public static String removeAll(final String text, final Pattern regex) {
139139
* if the regular expression's syntax is invalid
140140
*
141141
* @see #replaceAll(String, String, String)
142-
* @see #removePattern(String, String)
142+
* @see #removePattern(CharSequence, String)
143143
* @see String#replaceAll(String, String)
144144
* @see java.util.regex.Pattern
145145
* @see java.util.regex.Pattern#DOTALL
@@ -256,6 +256,39 @@ public static String removeFirst(final String text, final String regex) {
256256
* @see String#replaceAll(String, String)
257257
* @see Pattern#DOTALL
258258
*/
259+
public static CharSequence removePattern(final CharSequence text, final String regex) {
260+
return replacePattern(text, regex, StringUtils.EMPTY);
261+
}
262+
263+
/**
264+
* Removes each substring of the source String that matches the given regular expression using the DOTALL option.
265+
*
266+
* This call is a {@code null} safe equivalent to:
267+
* <ul>
268+
* <li>{@code text.replaceAll(&quot;(?s)&quot; + regex, StringUtils.EMPTY)}</li>
269+
* <li>{@code Pattern.compile(regex, Pattern.DOTALL).matcher(text).replaceAll(StringUtils.EMPTY)}</li>
270+
* </ul>
271+
*
272+
* <p>A {@code null} reference passed to this method is a no-op.</p>
273+
*
274+
* <pre>{@code
275+
* StringUtils.removePattern(null, *) = null
276+
* StringUtils.removePattern("any", (String) null) = "any"
277+
* StringUtils.removePattern("A<__>\n<__>B", "<.*>") = "AB"
278+
* StringUtils.removePattern("ABCabc123", "[a-z]") = "ABC123"
279+
* }</pre>
280+
*
281+
* @param text
282+
* the source string
283+
* @param regex
284+
* the regular expression to which this string is to be matched
285+
* @return The resulting {@link String}
286+
* @see #replacePattern(CharSequence, String, String)
287+
* @see String#replaceAll(String, String)
288+
* @see Pattern#DOTALL
289+
* @deprecated use {@link #removePattern(CharSequence, String)}.
290+
*/
291+
@Deprecated
259292
public static String removePattern(final String text, final String regex) {
260293
return replacePattern(text, regex, StringUtils.EMPTY);
261294
}

src/test/java/org/apache/commons/lang3/RegExUtilsTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,24 @@ public void testRemoveFirst_StringString() {
125125
}
126126

127127
@Test
128-
public void testRemovePattern_StringString() {
128+
public void testRemovePattern() {
129+
assertNull(RegExUtils.removePattern((CharSequence) null, ""));
130+
assertEquals("any", RegExUtils.removePattern((CharSequence) "any", (String) null));
131+
132+
assertEquals("", RegExUtils.removePattern((CharSequence) "", ""));
133+
assertEquals("", RegExUtils.removePattern((CharSequence) "", ".*"));
134+
assertEquals("", RegExUtils.removePattern((CharSequence) "", ".+"));
135+
136+
assertEquals("AB", RegExUtils.removePattern((CharSequence) "A<__>\n<__>B", "<.*>"));
137+
assertEquals("AB", RegExUtils.removePattern((CharSequence) "A<__>\\n<__>B", "<.*>"));
138+
assertEquals("", RegExUtils.removePattern((CharSequence) "<A>x\\ny</A>", "<A>.*</A>"));
139+
assertEquals("", RegExUtils.removePattern((CharSequence) "<A>\nxy\n</A>", "<A>.*</A>"));
140+
141+
assertEquals("ABC123", RegExUtils.removePattern((CharSequence) "ABCabc123", "[a-z]"));
142+
}
143+
144+
@Test
145+
public void testRemovePatternDeprecated() {
129146
assertNull(RegExUtils.removePattern(null, ""));
130147
assertEquals("any", RegExUtils.removePattern("any", (String) null));
131148

0 commit comments

Comments
 (0)