ppkarwasz commented on code in PR #1382:
URL: https://github.com/apache/commons-lang/pull/1382#discussion_r2094204002
##########
src/main/java/org/apache/commons/lang3/RegExUtils.java:
##########
@@ -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<String> findMatches(CharSequence text, Pattern pattern) {
Review Comment:
```suggestion
public static List<String> findAll(CharSequence text, Pattern pattern) {
```
This is an utility class, so all the methods must be `static`.
_Nitpick_: Naming is certainly on of the hardest tasks. Since all the
methods in this class deal with matching and have names like `replaceAll`,
`removeAll`, I would call this method `findAll`.
##########
src/main/java/org/apache/commons/lang3/RegExUtils.java:
##########
@@ -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<String> findMatches(CharSequence text, Pattern pattern) {
+ if (text == null || pattern == null){
+ return Collections.emptyList();
+ }
+
+ List<String> 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<String> 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<String> findMatchesModifiable(CharSequence text, Pattern
pattern){
Review Comment:
-1
We should not start multiplying methods.
##########
src/test/java/org/apache/commons/lang3/RegExUtilsTest.java:
##########
@@ -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();
+ }
Review Comment:
Utility classes are never instantiated.
##########
src/test/java/org/apache/commons/lang3/RegExUtilsTest.java:
##########
@@ -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() {
Review Comment:
Can you use parameterized logging instead of multiple methods with the same
logic?
##########
src/main/java/org/apache/commons/lang3/RegExUtils.java:
##########
@@ -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<String> findMatches(CharSequence text, Pattern pattern) {
+ if (text == null || pattern == null){
+ return Collections.emptyList();
+ }
+
+ List<String> 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) {
Review Comment:
Looking at the remaining methods of this class, each method with signature
`(CharSequence, Pattern)` has a corresponding method with signature `(String,
String)`. Therefore I would define a method:
```java
public static List<String> findAll(String text, String regex)
```
I don't think it is useful to have both a method returning `List<String>`
and `String[]`. The return type should be coherent.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]