Search for a regexp in a java arraylist

前端 未结 6 1749
失恋的感觉
失恋的感觉 2020-12-14 19:13
ArrayList  list = new ArrayList(); 
list.add(\"behold\");
list.add(\"bend\");
list.add(\"bet\");
list.add(\"bear\");
list.add(\"beat\");
list.add(\"bec         


        
相关标签:
6条回答
  • 2020-12-14 19:45

    This is a one liner in guava:

    final Iterable<String> matches = Iterables.filter(myStrings, Predicates.contains(Pattern.compile("myPattern")));
    
    for (final String matched : matches) {
       ...
    }
    
    0 讨论(0)
  • 2020-12-14 19:50

    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.

    0 讨论(0)
  • 2020-12-14 19:51

    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
    }
    
    0 讨论(0)
  • 2020-12-14 19:58

    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.

    0 讨论(0)
  • 2020-12-14 20:03

    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.

    0 讨论(0)
  • 2020-12-14 20:04

    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.

    0 讨论(0)
提交回复
热议问题