Filtering records of a String Array by space-separated words in Java

4

How do I filter the records of a Array by using the search terms separated by spaço ?

Example:

I have a% of String% with the following records.

  • bottle opener
  • box with blue padlock
  • 30mm brass padlock with 03 keys
  • 100mm screwdriver
  • padlock game with 02 keys
  • cloth to clean sink
  • garden faucet

In the filter field, the user types only cha cha , this search should only return the records.

  • 30mm brass padlock with 03 keys
  • padlock game with 02 keys

To try to solve this problem, a function with Array was created to read record by for . For each record I tried to use the regular expression "(cade | chav)" it filters the following products;

  • box with blue padlock
  • 30mm brass padlock w / 03 yellow keys
  • 100mm screwdriver
  • padlock game with 02 keys

But bold items should not appear.

Another thing that should be taken into account, the user can type only "padlock", or "cad brass keys", or "blue" or with one or more words in the filter, until then I know that I will have to assemble the expression dynamically as "(cade | chav)" or "(cad | brass | keys)" or "(azu)" etc.

If there is another way to solve this problem in java without using regular expression it will also be coming.

    
asked by anonymous 07.05.2015 / 20:34

2 answers

2

The following regular expression can be used:

(?=.*chav)(?=.*cade)

And so on for every word. I tested it here and it worked: link

    
07.05.2015 / 22:24
1

The problem is probably in the regular expression you are using. Here is a small example (you may have to make some adjustments, but I think it should work)

import java.util.regex.Pattern;
import java.util.ListIterator;
import java.util.ArrayList;

class Matcher
{
    public static ArrayList<String> getMatchingStrings(ArrayList<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;
   }

   public static String buildRegex(ArrayList<String> listWords) {
       StringBuilder builder = new StringBuilder();

       for (String s:listWords) {
           builder.append("(?=.*" + s + "\w*)");
       }
       builder.append(".+");
       return builder.toString();
   }

   public static void main (String[] args) throws java.lang.Exception
   {
        ArrayList<String> lista = new ArrayList<String>();
        lista.add("abridor de garrafa");
        lista.add("caixa com cadeado azul");
        lista.add("cadeado latão 30mm c/03 chaves");
        lista.add("chave de fenda 100mm");
        lista.add("jogo de cadeado c/02 chaves");
        lista.add("pano para limpar pia");
        lista.add("torneira de jardim");

        ArrayList<String> listWords = new ArrayList<String>();
        listWords.add("cade");
        listWords.add("chav");

        String regex = buildRegex(listWords);

        //ArrayList<String> res = getMatchingStrings(lista, "(?=.*chav\w*)(?=.*cadea\w*).+");
        ArrayList<String> res = getMatchingStrings(lista, regex);

        for (String s:res) {
            System.out.println(s);
        }

    }
}
    
07.05.2015 / 22:58