Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Filtering List Entries by Regex

My code looks like this:

List<String> filterList(List<String> list, String regex) {
  List<String> result = new ArrayList<String>();
  for (String entry : list) {
    if (entry.matches(regex)) {
      result.add(entry);
    }
  }
  return result;
}

It returns a list that contains only those entries that match the regex. I was wondering if there was a built in function for this along the lines of:

List<String> filterList(List<String> list, String regex) {
  List<String> result = new ArrayList<String>();
  result.addAll(list, regex);
  return result;
}
like image 899
Benjy Kessler Avatar asked Jul 17 '14 07:07

Benjy Kessler


People also ask

How to list files using regular expressions to filter them in Java?

In order to list Files using regular expressions to filter them we have set the example below: Class DirFilter implements the FilenameFilter. It has a Pattern that is initialized by compiling a String regular expression to a Pattern, using compile (String regex) API method of Pattern. It also has a method boolean accept (File dir, String name).

Is there a way to filter a list in Java?

List<String> filterList (List<String> list, String regex) { return list.stream ().filter (s -> s.matches (regex)).collect (Collectors.toList ()); } Alas I am using Java 7. Show activity on this post. Google's Java library (Guava) has an interface Predicate<T> which might be pretty useful for your case.

How do I filter a list by regex in Python?

List<String> filterList (List<String> list, String regex) { List<String> result = new ArrayList<String> (); for (String entry : list) { if (entry.matches (regex)) { result.add (entry); } } return result; } It returns a list that contains only those entries that match the regex .

How to filter a list with a predicate in Java 8?

First the list is converted into a stream. This stream is then filtered with the predicate and the stream is converted back into a list with the collect () method. Thanks to the lambda notation of Java 8, the Predicate can also be passed as a simple function, which shortens the expression even more: makes the filtering even clearer.


3 Answers

In addition to the answer from Konstantin: Java 8 added Predicate support to the Pattern class via asPredicate, which calls Matcher.find() internally:

Pattern pattern = Pattern.compile("...");

List<String> matching = list.stream()
                            .filter(pattern.asPredicate())
                            .collect(Collectors.toList());

Pretty awesome!

like image 139
scheffield Avatar answered Oct 23 '22 11:10

scheffield


In java 8 you can do something like this using new stream API:

List<String> filterList(List<String> list, String regex) {
    return list.stream().filter(s -> s.matches(regex)).collect(Collectors.toList());
}
like image 37
Konstantin V. Salikhov Avatar answered Oct 23 '22 11:10

Konstantin V. Salikhov


Google's Java library(Guava) has an interface Predicate<T> which might be pretty useful for your case.

static String regex = "yourRegex";

Predicate<String> matchesWithRegex = new Predicate<String>() {
        @Override 
        public boolean apply(String str) {
            return str.matches(regex);
        }               
};

You define a predicate like the one above and then filter your list based on this predicate with a single-line code:

Iterable<String> iterable = Iterables.filter(originalList, matchesWithRegex);

And to convert the iterable to a list, you can again use Guava:

ArrayList<String> resultList = Lists.newArrayList(iterable);
like image 4
Juvanis Avatar answered Oct 23 '22 10:10

Juvanis