title: Regex performance issues - Profanity filter
body: <p>I'm making a profanity filter (bad idea I know), and I'm trying to do it with regex in Java.</p>
  
  <p>Right now here's my regex example string, this would filter 2 words, foo and bar.</p>
  
  <pre><code>(?i)f(?&gt;[.,;:*`'^+~\\/#|]*+|.)o(?&gt;[.,;:*`'^+~\\/#|]*+|.)o|b(?&gt;[.,;:*`'^+~\\/#|]*+|.)a(?&gt;[.,;:*`'^+~\\/#|]*+|.)r
  </code></pre>
  
  <p>Basically, I have it ignore case, then I put <code>(?&gt;[.,;:*'^+~\\/#|]*+|.)</code> in between each letter of a curse word, and <code>|</code> between each complete curse word regex.</p>
  
  <p>It works, but it's sorta slow.</p>
  
  <p>If I have 6 words in the filter, it will filter a fairly long string (500 characters) in 939,548 nanoseconds. When I have 12, it just about doubles.</p>
  
  <p>So, about 1ms per 6 curse words with this. But my filter will have hundreds (400 or so).
  Calculating this, it would take about 66ms to filter this long string.</p>
  
  <p>This is a chat server I'm building, and if I have lots of users on (say, 5,000) and 1 out of 5 are chatting in 1 second (1,000 chat messages) I need to filter a message in about 1ms.</p>
  
  <p>Am I asking too much of regexps? Would it be faster to make my own specialized type of filter by hand? Are there ways to optimize this?</p>
  
  <p>I am precompiling the regex.</p>
  
  <p>If you want to see the effect of this regex <a href="http://regexr.com?30454" rel="nofollow">http://regexr.com?30454</a></p>
  
  <p><strong>Update:</strong> Another thing I could to is have chat messages filtered client side in actionscript.</p>
  
  <p><strong>Update:</strong> I believe the only way to achieve such degree of performance would be a hand-coded solution without using regexps sadly, so I'll have to do a more basic filter.</p>