title: java escape parenthesis
body: <p>i have this little class to make a multiple replace on a string:</p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Map;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  import org.apache.commons.lang.StringUtils;
  public class MultipleReplace {
      public static void main(String[] args) {
          Map&lt;String,String&gt; tokens = new HashMap&lt;String,String&gt;();
          tokens.put(":asd:", "&lt;img src=asd.gif&gt;");
          tokens.put(":)", "&lt;img src=sorriso.gif&gt;");
          String template = ":asd: bravo! :)";
          String patternString = "(" + StringUtils.join(tokens.keySet(), "|") + ")";
          Pattern pattern = Pattern.compile(patternString);
          Matcher matcher = pattern.matcher(template);
          StringBuffer sb = new StringBuffer();
          while(matcher.find()) {
              matcher.appendReplacement(sb, tokens.get(matcher.group(1)));
          }
          matcher.appendTail(sb);
  
          System.out.println(sb.toString());
      }
  }
  </code></pre>
  
  <p>The problem is on the second replace, where i have a parenthesis that result in: </p>
  
  <p><strong>Exception in thread "main" java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 8
  (:)|:asd:)</strong></p>
  
  <p>How i can escape the parenthesis? 
  Or, can you suggest an alternative to do this multiple replace?</p>
  
  <p>Thank you very much and sorry for my english :)</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Escaping with backslash ')' doesn't work too, it won't compile: </p>
  
  <p><strong>"Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )"</strong></p>
  
  <p><strong>NEW EDIT</strong></p>
  
  <p>using two backslashes compile, but doesn't do the replacement.</p>
  
  <p><strong>LAST EDIT</strong></p>
  
  <p>Finally found the solution, using Pattern.quote while building the pattern. Have to use an iterator to do the loop.</p>
  
  <p>Here the correct code:</p>
  
  <pre><code>package string;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  public class MultipleReplace {
      @SuppressWarnings("rawtypes")
      public static void main(String[] args) {
          Map&lt;String,String&gt; tokens = new HashMap&lt;String,String&gt;();
          tokens.put(":asd:", "&lt;img src=asd.gif&gt;");
          tokens.put(":)", "&lt;img src=sorriso.gif&gt;");
          String template = ":asd: bravo! :)";
          Iterator it = tokens.entrySet().iterator();
          String patternString = "(";
          while (it.hasNext()) {
              Map.Entry pairs = (Map.Entry)it.next();
              System.out.println(pairs.getKey() + " = " + pairs.getValue());
              patternString = patternString +Pattern.quote((String) pairs.getKey());
              if (it.hasNext())
              {
                  patternString = patternString + "|";
              }
          }
          patternString = patternString + ")";
          System.out.println(patternString);
          Pattern pattern = Pattern.compile(patternString);
          Matcher matcher = pattern.matcher(template);
          StringBuffer sb = new StringBuffer();
          while(matcher.find()) {
              matcher.appendReplacement(sb, tokens.get(matcher.group(1)));
          }
          matcher.appendTail(sb);
          System.out.println(sb.toString());
      }
  }
  </code></pre>
  
  <p>Please comment on it if i can improve the work! Thank you very much!</p>