diff --git a/src/main/java/org/apache/commons/lang3/RegExUtils.java b/src/main/java/org/apache/commons/lang3/RegExUtils.java index dc3351d03f6..3b037622165 100644 --- a/src/main/java/org/apache/commons/lang3/RegExUtils.java +++ b/src/main/java/org/apache/commons/lang3/RegExUtils.java @@ -16,9 +16,13 @@ */ package org.apache.commons.lang3; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; /** * Helpers to process Strings using regular expressions. @@ -751,4 +755,67 @@ private static String toStringOrNull(final CharSequence text) { public RegExUtils() { // empty } + + /** + * Finds all matches in the given text according to the specified pattern. + * + * @param text the text to search for matches + * @param pattern the pattern to search for + * @return an unmodifiable list of found matches; returns an empty List if text or pattern is null + */ + public List findMatches(CharSequence text, Pattern pattern) { + if (text == null || pattern == null){ + return Collections.emptyList(); + } + + List matches = new ArrayList<>(); + Matcher matcher = pattern.matcher(text); + + while (matcher.find()) { + matches.add(matcher.group()); + } + + return Collections.unmodifiableList(matches); + } + + /** + * Finds all matches in the given text according to the specified pattern + * and returns them as an array of strings. + * + * @param text the text to search for matches + * @param regex the regular expression pattern + * @return an array of found matches; returns an empty array if text or regex is null + */ + public String[] findMatchesAsArray(CharSequence text, String regex) { + if (text == null ||regex == null){ + return new String[0]; + } + try { + Pattern pattern = Pattern.compile(regex); + List matches = findMatches(text, pattern); + return matches.toArray(new String[0]); + }catch (PatternSyntaxException e){ + return new String[0]; + } + } + + /** + * Finds all matches in the given text according to the specified pattern + * and returns them as a modifiable ArrayList. + * + * @param text the text to search for matches + * @param pattern the pattern to search for + * @return a modifiable ArrayList of found matches; returns an empty List if text or pattern is null + */ + public List findMatchesModifiable(CharSequence text, Pattern pattern){ + if (text == null || pattern == null){ + return new ArrayList<>(); + } + Matcher matcher = pattern.matcher(text); + List matches = new ArrayList<>(); + while (matcher.find()){ + matches.add(matcher.group()); + } + return matches; + } } diff --git a/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java b/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java index af3ee9df0a3..51cda73f421 100644 --- a/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java @@ -16,16 +16,18 @@ */ 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.Arrays; +import java.util.List; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import javax.annotation.RegEx; + +import static org.junit.jupiter.api.Assertions.*; + /** * Tests {@link RegExUtils}. */ @@ -379,4 +381,149 @@ public void testReplacePatternDeprecated() { assertEquals("Lorem_ipsum_dolor_sit", RegExUtils.replacePattern("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2")); } + private RegExUtils utils; + + @BeforeEach + void setUp() { + utils = new RegExUtils(); + } + + @Test + public void testFindMultipleMatches() { + Pattern pattern = Pattern.compile("\\d+"); + List result = utils.findMatches("asd 123 fgd 456 zxc", pattern); + assertEquals(Arrays.asList("123", "456"), result); + } + + @Test + public void testFindNoMatches() { + Pattern pattern = Pattern.compile("\\d+"); + List result = utils.findMatches("asd zxc", pattern); + assertTrue(result.isEmpty()); + } + + @Test + void testFindMatchesWhenTextIsEmpty() { + String[] result = utils.findMatchesAsArray("", "\\w+"); + assertArrayEquals(new String[0], result); + } +// @Test +// public void testShouldReturnArrayList() { +// Pattern pattern = Pattern.compile("\\w+"); +// List result = utils.findMatches("test", pattern); +// assertEquals(Arrays.asList("test"), result); +// assertTrue(result instanceof ArrayList); +// } + + @Test + public void testFindMatchesWhenTextIsNull() { + Pattern pattern = Pattern.compile("test"); + List result = utils.findMatches(null, pattern); + assert (result).isEmpty(); + } + + @Test + public void testFindMatchesWhenPatternIsNull() { + List result = utils.findMatches("test", null); + assertTrue(result.isEmpty()); + } + + @Test + public void testFindMatchesWithEmptyText() { + Pattern pattern = Pattern.compile("\\d+"); + List result = utils.findMatches("", pattern); + assertTrue(result.isEmpty()); + } + + @Test + public void testFindMultipleMatchesAsArray() { + String[] result = utils.findMatchesAsArray("test 123 test", "\\w+"); + assertArrayEquals(new String[]{"test", "123", "test"}, result); + } + + @Test + void testFindMatchesAsArrayWhenNoMatches() { + String[] result = utils.findMatchesAsArray("abc", "\\d"); + assertArrayEquals(new String[0], result); + } + + @Test + void testFindMatchesAsArrayWhenTextIsNull() { + String[] result = utils.findMatchesAsArray(null, "\\w+"); + assertArrayEquals(new String[0], result); + } + + @Test + void testFindMatchesAsArrayWhenTextIsEmpty() { + String[] result = utils.findMatchesAsArray("", "\\w+"); + assertArrayEquals(new String[0], result); + } + + @Test + void testFindMatchesAsArrayWithInvalidRegex() { + String[] result = utils.findMatchesAsArray("text", "[a-z"); + assertArrayEquals(new String[0], result); + } + + @Test + public void testReturnedListIsUnmodifiable() { + Pattern pattern = Pattern.compile("\\w+"); + List result = utils.findMatches("test", pattern); + + try { + result.add("should fail"); + fail("Expected UnsupportedOperationException"); + } catch (UnsupportedOperationException e) {} + } + + @Test + public void testFindMultipleMatchesAsArrayList(){ + Pattern pattern = Pattern.compile("\\d+"); + List result = utils.findMatchesModifiable("asd 123 fgd 456 zxc", pattern); + assertEquals(Arrays.asList("123", "456"), result); + } + + @Test + public void testShouldAddElementInFindMatchesModifiable(){ + Pattern pattern = Pattern.compile("\\w+"); + List result = utils.findMatchesModifiable("asd test 123", pattern); + result.add("text"); + assertEquals(Arrays.asList("asd", "test", "123", "text"), result); + } + + @Test + public void testShouldRemoveElementInFindMatchesModifiable(){ + Pattern pattern = Pattern.compile("\\w+"); + List result = utils.findMatchesModifiable("asd test 123", pattern); + result.remove("123"); + assertEquals(Arrays.asList("asd","test"), result); + } + + @Test + public void testFindNoMatchesAsArrayList() { + Pattern pattern = Pattern.compile("\\d+"); + List result = utils.findMatchesModifiable("asd zxc", pattern); + assertTrue(result.isEmpty()); + } + + @Test + void testFindMatchesAsArrayListWhenTextIsEmpty() { + Pattern pattern = Pattern.compile("\\d+"); + Listresult = utils.findMatchesModifiable("", pattern); + assertTrue(result.isEmpty()); + } + + @Test + void testFindMatchesAsArrayListWhenTextIsNull() { + Pattern pattern = Pattern.compile("\\d+"); + Listresult = utils.findMatchesModifiable(null, pattern); + assertTrue(result.isEmpty()); + } + + @Test + void testFindMatchesAsArrayListWhenPatternIsNull(){ + Listresult = utils.findMatchesModifiable("asd 123", null); + assertTrue(result.isEmpty()); + } + }