This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 944d6e10a Refactor and add RegExUtils.dotAll() and dotAllMatcher()
944d6e10a is described below
commit 944d6e10a47d1f662b7bda5fd712dfb5440a36cf
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Apr 1 11:40:07 2023 -0400
Refactor and add RegExUtils.dotAll() and dotAllMatcher()
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/lang3/RegExUtils.java | 27 +++++++++++++++++++++-
.../org/apache/commons/lang3/RegExUtilsTest.java | 13 ++++++++++-
3 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4100d6bce..4e7c2ea42 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -192,6 +192,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add NumberRange, DoubleRange, IntegerRange, LongRange.</action>
<action type="add" dev="ggregory" due-to="Diego
Marcilio, Bruno P. Kinoshita, Gary Gregory">Add missing exception javadoc/tests
for some null arguments #869.</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add ClassLoaderUtils.getSystemURLs() and getThreadURLs().</action>
+ <action type="add" dev="ggregory" due-to="Gary
Gregory">Add RegExUtils.dotAll() and dotAllMatcher().</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot,
XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.10 #742, #752,
#764, #833, #867, #959, #964.</action>
<action type="update" dev="ggregory" due-to="Dependabot,
Gary Gregory">Bump actions/checkout from 2 to 3.1.0 #819, #825, #859,
#963.</action>
diff --git a/src/main/java/org/apache/commons/lang3/RegExUtils.java
b/src/main/java/org/apache/commons/lang3/RegExUtils.java
index 58fa38c4e..2c03a4d7e 100644
--- a/src/main/java/org/apache/commons/lang3/RegExUtils.java
+++ b/src/main/java/org/apache/commons/lang3/RegExUtils.java
@@ -16,6 +16,7 @@
*/
package org.apache.commons.lang3;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
@@ -25,6 +26,30 @@ import java.util.regex.Pattern;
*/
public class RegExUtils {
+ /**
+ * Compiles the given regular expression into a pattern with the {@link
Pattern#DOTALL} flag.
+ *
+ * @param regex The expression to be compiled
+ * @return the given regular expression compiled into a pattern with the
{@link Pattern#DOTALL} flag.
+ * @since 3.13.0
+ */
+ public static Pattern dotAll(final String regex) {
+ return Pattern.compile(regex, Pattern.DOTALL);
+ }
+
+ /**
+ * Compiles the given regular expression into a pattern with the {@link
Pattern#DOTALL} flag, then creates a matcher that will match the given text
against
+ * this pattern.
+ *
+ * @param regex The expression to be compiled.
+ * @param text The character sequence to be matched.
+ * @return A new matcher for this pattern.
+ * @since 3.13.0
+ */
+ public static Matcher dotAllMatcher(final String regex, final String text)
{
+ return dotAll(regex).matcher(text);
+ }
+
/**
* Removes each substring of the text String that matches the given
regular expression pattern.
*
@@ -452,7 +477,7 @@ public class RegExUtils {
if (ObjectUtils.anyNull(text, regex, replacement)) {
return text;
}
- return Pattern.compile(regex,
Pattern.DOTALL).matcher(text).replaceAll(replacement);
+ return dotAllMatcher(regex, text).replaceAll(replacement);
}
}
diff --git a/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java
b/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java
index 768c788b0..f10b16aca 100644
--- a/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.lang3;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
@@ -26,10 +27,20 @@ import java.util.regex.PatternSyntaxException;
import org.junit.jupiter.api.Test;
/**
- * Unit tests for methods of {@link org.apache.commons.lang3.RegExUtils} which
been moved to their own test classes.
+ * Tests {@link org.apache.commons.lang3.RegExUtils}.
*/
public class RegExUtilsTest extends AbstractLangTest {
+ @Test
+ public void testDotAll() {
+
assertTrue(RegExUtils.dotAll("<A>.*</A>").matcher("<A>\nxy\n</A>").matches());
+ }
+
+ @Test
+ public void testDotAllMatcher() {
+ assertTrue(RegExUtils.dotAllMatcher("<A>.*</A>",
"<A>\nxy\n</A>").matches());
+ }
+
@Test
public void testRemoveAll_StringPattern() {
assertNull(RegExUtils.removeAll(null, Pattern.compile("")));