title: Building regex to catch word
body: <p>Been trying to solve this problem for a few days now and just can't see what's incorrect about it. </p>
  
  <p>The idea of the function is to catch spam mail and as such it takes as parameter an email to be checked and a string array containing words to be checked against and build a regex out of all the words. Then using the built regex strings check the body and subject of the email if it contains any of the words, if it does it shouldn't post it. </p>
  
  <p>It catches the spam mails that I have tried but it also catches all other mails.</p>
  
  <pre><code>private boolean isSpam(Mail mail, String[] filter){
      StringBuilder sb = new StringBuilder();
  
      // Build the regex String for each string in filter
      for(int i=0;i&lt;filter.length;i++){
          sb.append("(?i).*");
          for(int ii = 0;ii&lt;filter[i].length();ii++){
              sb.append("[" + filter[i].charAt(ii) + "\\s]*"); // [x\\s]*; x = current letter
          }
          filter[i] = sb.toString();
          sb.delete(0, sb.length());
      }
  
      for(int i=0;i&lt;filter.length;i++){
          if(mail.getSubject().matches(filter[i]) ||  mail.getMessage().matches(filter[i])){
              return true;
          }
      }
      return false;
  }
  </code></pre>
  
  <p>The regex string for ex. <strong>spam</strong> would look like: <strong>(?i).<em>[s\\s]</em>[p\\s]<em>[a\\s]</em>[m\\s]*</strong></p>