Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8719899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:54:32+00:00 2026-06-13T06:54:32+00:00

I have the following class PlaceHolderConverter used to parse string like my {} are

  • 0

I have the following class PlaceHolderConverter used to parse string like "my {} are beautiful" into strings with filled variables.

For instance new PlaceHolderConverter("\\{\\}").format("my {} are beautiful", "flowers") will return the string "my flowers are beautiful".

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();
    }
}

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 Pattern.quote() but without results.

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 );
    }

}

I also to tried to manually escape the dollar before using it in regexp, with something like .replaceAll("\\$", "\\\\$") but it seems that replaceAll dislike to have to arg1 included in arg2.

How I can fix that ?

Patch can be provided here https://gist.github.com/3937872

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:54:34+00:00Added an answer on June 13, 2026 at 6:54 am

    When replacing a fixed string, there is no need to call the regex methods on String because there is a simpler one: input.replace("$", "\\$");. With this method you won’t have any of the troubles caused by the special meaning of the dollar sign, and it will be (very slightly) faster as a bonus.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code class User attr_accessor :name end u = User.new u.name =
I have the following class public class MenuVeiculo { public string Nome { get;
I have 3 following classes: public class BaseProperty1{ public string Property1 {get; set;} }
Lets say i have following class public class abc { int id; string name;
I have following code: class TR_AgentInfo : public tuple< long long, //AgentId string, //AgentIp
I have following classes: class Department { private string departmentId; private string departmentName; private
If have following class public abstract class MyBaseClass : BaseClass { public override string
I have the following class: public abstract class AbstractParent { static String method() {
i have following class: public class FirstClass { public FirstClass() { list = new
I have following string Class (102) (401) and Class (401) i want to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.