Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for a regexp in a java arraylist

ArrayList <String> list = new ArrayList(); 
list.add("behold");
list.add("bend");
list.add("bet");
list.add("bear");
list.add("beat");
list.add("become");
list.add("begin"); 

There is a way to search for the regexp bea.* and get the indexes like in ArrayList.indexOf ?

EDIT: returning the items is fine but I need something with more performance than a Linear search

like image 878
kmilo Avatar asked Nov 20 '08 21:11

kmilo


People also ask

How do I match a regex pattern?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .

How do you check if a string matches a pattern in Java?

You can use the Pattern. matches() method to quickly check if a text (String) matches a given regular expression. Or you can compile a Pattern instance using Pattern. compile() which can be used multiple times to match the regular expression against multiple texts.


3 Answers

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
}
like image 176
DJClayworth Avatar answered Oct 19 '22 10:10

DJClayworth


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.

like image 30
Herms Avatar answered Oct 19 '22 09:10

Herms


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.

like image 33
Jacob Mattison Avatar answered Oct 19 '22 09:10

Jacob Mattison