title: How do I sanitize input before making a regex out of it?
body: <p>I'm making a Java program that parses the user's input with a regex. For instance, if the user inputs <code>/me eats</code>, it should match the <code>/me</code> and replace it with <code>&lt;move&gt;</code>. However, Java isn't properly matching because <code>/</code> is a special character to regexes. <strong>How do I automatically replace all the various special Java regex characters with escapes?</strong></p>
  
  <p>For instance:</p>
  
  <ul>
  <li><code>/me</code> becomes <code>\/me</code></li>
  <li><code>*</code> becomes <code>\*</code></li>
  <li><code>[</code> becomes <code>\[</code></li>
  <li>and so on...</li>
  </ul>
  
  <p><em>before</em> it's put into <code>Pattern.compile</code>.</p>
  
  <p>This is not a command system. I am allowing users to specify how to denote a roleplaying move. If it helps, here is a mockup of how the user specifies what they consider a roleplay move:</p>
  
  <p><a href="https://docs.google.com/drawings/d/1hlvw2FpkLHvn3InYo3SG7ewxn8y3Z3MAgj3G_Ja_lDo/edit?usp=sharing" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mV66g.png" alt="A mockup of the preferences pane that would control this system"></a></p>