title: Best way to deal with dollar character in java regex
body: <p>I have the following class <code>PlaceHolderConverter</code> used to parse string like <code>"my {} are beautiful"</code> into strings with filled variables.</p>
  
  <p>For instance <code>new PlaceHolderConverter("\\{\\}").format("my {} are beautiful", "flowers")</code> will return the string <code>"my flowers are beautiful"</code>.</p>
  
  <pre class="lang-java prettyprint-override"><code>package something;
  
  
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  public class PlaceHolderConverter
  {
      public Pattern lookForVar;
  
      public PlaceHolderConverter( String placeHolder )
      {
          this.lookForVar = Pattern.compile( placeHolder );
      }
  
      public String format( String text, String... args )
      {
          if ( args == null || args.length == 0 )
          {
              return text;
          }
          StringBuffer stringBuffer = new StringBuffer();
          Matcher matcher = lookForVar.matcher( text );
          short varCount = 0;
          while ( matcher.find() )
          {
              matcher.appendReplacement( stringBuffer, args[varCount++] );
          }
          matcher.appendTail( stringBuffer );
          return stringBuffer.toString();
      }
  }
  </code></pre>
  
  <p>As you can see in the following tests, I have issue with the special character dollar because it's a special character for java regex.
  I tried to solve that with <code>Pattern.quote()</code> but without results.</p>
  
  <pre class="lang-java prettyprint-override"><code>package something;
  
  import org.junit.Assert;
  import org.junit.Before;
  import org.junit.Test;
  
  import java.util.regex.Pattern;
  
  public class PlaceHolderConverterTest
  {
      private PlaceHolderConverter placeHolderConverter;
  
      @Before
      public void before()
      {
          placeHolderConverter = new PlaceHolderConverter( "\\{\\}" );
      }
  
      @Test // SUCCESS
      public void whenStringArgsThenReplace()
      {
          String result = placeHolderConverter.format( "My {} are beautifull", "flowers" );
          Assert.assertEquals( "My flowers are beautifull", result );
      }
  
      @Test // FAIL IllegalArgumentException illegal group reference while calling appendReplacement
      public void assertEscapeDollar()
      {
          String result = placeHolderConverter.format( "My {} are beautiful", "flow$ers" );
          Assert.assertEquals( "My flow$ers are beautiful", result );
      }
  
      @Test // FAIL IllegalArgumentException illegal group reference while calling appendReplacement
      public void assertEscapeDollarWithQuote()
      {
          String result = placeHolderConverter.format( "My {} are beautiful", Pattern.quote("flow$ers") );
          Assert.assertEquals( "My flow$ers are beautiful", result );
      }
  
  }
  </code></pre>
  
  <p>I also to tried to manually escape the dollar before using it in regexp, with something like <code>.replaceAll("\\$", "\\\\$")</code> but it seems that <code>replaceAll</code> dislike to have to arg1 included in arg2.</p>
  
  <p>How I can fix that ?</p>
  
  <p>Patch can be provided here <a href="https://gist.github.com/3937872" rel="nofollow noreferrer">https://gist.github.com/3937872</a></p>