title: Running multiple regex patterns on String
body: <p>Assuming I have a <code>List&lt;String&gt;</code> and an empty <code>List&lt;Pattern&gt;</code>, is this the best way to handle making the words in the String into Pattern objects;</p>
  
  <pre><code>for(String word : stringList) {
      patterns.add(Pattern.compile("\\b(" + word + ")\\b);
  }
  </code></pre>
  
  <p>And then to run this on a string later;</p>
  
  <pre><code>for(Pattern pattern : patterns) {
      Matcher matcher = pattern.matcher(myString);
      if(matcher.matches()) {
           myString = matcher.replaceAll("String[$1]");
      }
  }
  </code></pre>
  
  <p>The replaceAll bit is just an example, but $1 would be used most of the the time when I use this.</p>
  
  <p>Is there a more efficient way? Because I feel like this is somewhat clunky. I'm using 80 Strings in the list by the way, though the Strings used are configurable, so there won't always be so many.</p>
  
  <p>This is designed to be somewhat of a swearing filter so I'll let you assume the words in the List,</p>
  
  <p>An example of input would be <code>"You're a &lt;curse&gt;"</code>, the output would be <code>"You're a *****"</code> for this word, though this may not always be the case and at some point I may be reading from a <code>HashMap&lt;String, String&gt;</code>where the key is the capture group and the value is the replacement.</p>
  
  <p>Example:</p>
  
  <pre><code>if(hashMap.get(matcher.group(1)) == null) { 
      // Can't test if \ is required. Used it here for safe measure.
      matcher.replaceAll("\*\*\*\*");
   } else {
      matcher.replaceAll(hashMap.get(matcher.group(1));
   }
  </code></pre>