ArrayList list = new ArrayList();
list.add(\"behold\");
list.add(\"bend\");
list.add(\"bet\");
list.add(\"bear\");
list.add(\"beat\");
list.add(\"bec
This is a one liner in guava:
final Iterable<String> matches = Iterables.filter(myStrings, Predicates.contains(Pattern.compile("myPattern")));
for (final String matched : matches) {
...
}
One option is to use Apache Commons CollectionUtils "select" method. You would need to create a Predicate object (an object with a single "evaluate" method that uses the regular expression to check for a match and return true or false) and then you can search for items in the list that match. However, it won't return the indexes, it will return a collection containing the items themselves.
Herms got the basics right. If you want the Strings and not the indexes then you can improve by using the Java 5 foreach loop:
import java.util.regex.Pattern;
import java.util.ListIterator;
import java.util.ArrayList;
/**
* Finds the index of all entries in the list that matches the regex
* @param list The list of strings to check
* @param regex The regular expression to use
* @return list containing the indexes of all matching entries
*/
List<String> getMatchingStrings(List<String> list, String regex) {
ArrayList<String> matches = new ArrayList<String>();
Pattern p = Pattern.compile(regex);
for (String s:list) {
if (p.matcher(s).matches()) {
matches.add(s);
}
}
return matches
}
I do not believe there is a Java API way of doing this, nor is there a Apache Commons way of doing this. It would not be difficult to roll your own however.
Is there a built-in method? Not that I know of. However, it should be rather easy to do it yourself. Here's some completely untested code that should give you the basic idea:
import java.util.regex.Pattern;
import java.util.ListIterator;
import java.util.ArrayList;
/**
* Finds the index of all entries in the list that matches the regex
* @param list The list of strings to check
* @param regex The regular expression to use
* @return list containing the indexes of all matching entries
*/
List<Integer> getMatchingIndexes(List<String> list, String regex) {
ListIterator<String> li = list.listIterator();
List<Integer> indexes = new ArrayList<Integer>();
while(li.hasNext()) {
int i = li.nextIndex();
String next = li.next();
if(Pattern.matches(regex, next)) {
indexes.add(i);
}
}
return indexes;
}
I might have the usage of Pattern and ListIterator parts a bit wrong (I've never used either), but that should give the basic idea. You could also do a simple for loop instead of the while loop over the iterator.
This will a thread revival, but might be useful to somebody. You might not need indexes, probably next step will do something on the items which matched the regex and therefore you asked for indexes. But you can use Java8 streams and lambda expression:
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.List;
...
var pattern = Pattern.compile(define); // var is Java 10 feature
List<String> list = originalList
.stream()
.filter(e -> pattern.matcher(e).matches())
.collect(Collectors.toList());
You can take the original list, convert it to a stream, run a filter on it which runs lambda to match your pattern and convert it back to a List. But you can keep it as stream and run .foreach on it with another lambda expression.