title: regex: How to escape backslashes and special characters?
body: <p>Is there a way to escape ( or protect )  special characters in a regular expression? </p>
  
  <p>What I would like to do is to create a simple regex tester:</p>
  
  <pre><code>import java.util.regex.*;
  class TestRegex { 
     public static void main( String ... args ) { 
         System.out.printf("%s ~= %s ? %s  %n" , args[0], args[1], Pattern.matches( args[0], args[1] ) );
     }
  }
  </code></pre>
  
  <p>Which works great to test my patterns before plug-in them into the program:</p>
  
  <pre><code>$java TestRegex "\d" 1
  \d ~= 1 ? true  
  $java TestRegex "\d" 12
  \d ~= 12 ? false  
  $java TestRegex "\d+" 12
  \d+ ~= 12 ? true  
  $java TestRegex "\d+" a12
  \d+ ~= a12 ? false  
  $java TestRegex "\d+" ""
  \d+ ~=  ? false  
  </code></pre>
  
  <p>The next thing I do is to use this pattern in my program, but each time I have to manually escape it:</p>
  
  <pre><code>Pattern p = Pattern.compile( /*copy pasted regex here */ );
  </code></pre>
  
  <p>And in this sample, substitute: <code>\d</code> with <code>\\d</code>. After a while this becomes very irritating . </p>
  
  <p>Q. How can I automatically escape these special characters? </p>