Filter String Array records by space-separated words in Java

Asked

Viewed 682 times

4

How do I filter the records of a Array using the search terms separated by space ?

Example:

I have a Array String with the following records.

  • bottle opener
  • box with blue padlock
  • Brass padlock 30mm c/03 keys
  • 100mm screwdriver
  • lock lock set c/02 keys
  • washcloth
  • garden faucet

In the filter field the user type only chav, this search should return only the records.

  • Brass padlock 30mm c/03 keys
  • lock lock set c/02 keys

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

  • box with blue padlock
  • Brass padlock 30mm c/03 keys yellow
  • 100mm screwdriver
  • lock lock set c/02 keys

However items in bold 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 I will have to mount 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 the regular expression it will also come.

  • At first I’d say you’d have to make a if(match) for every word present in the filter. Basically it would be if(match("cade") && match("chav").

2 answers

2


1

The problem is probably the regular expression you’re using. Follow 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);
        }

    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.