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 8386935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:04:54+00:00 2026-06-09T18:04:54+00:00

Currently, the replaceAll method of the String class, along with Matcher.replaceAll methods evaluate their

  • 0

Currently, the replaceAll method of the String class, along with Matcher.replaceAll methods evaluate their arguments as regular expressions.

The problem I am having is that the replacement string I am passing to either of these methods contains a dollar sign (which of course has special meaning in a regular expression). An easy work-around to this would be to pass my replacement string to ‘Matcher.quoteReplacement’ as this produces a string with literal characters, and then pass this sanitized string to replaceAll.

Unfortunately, I can’t do the above as I need to preserve the special characters as the resultant string is later used in operations where a reg ex is expected, and if I have escaped all the special characters this will break that contract.

Can someone please suggest a way I might achieve what I want to do? Many thanks.

EDIT: For clearer explanation, please find code example below:

String key = "USD";
String value = "$";

String content = "The figure is in USD";
String contentAfterReplacement;

contentAfterReplacement = content.replaceAll(key, value); //will throw an exception  as it will evaluate the $ in 'value' variable as special regex character

contentAfterReplacement = content.replaceAll(key, Matcher.quoteReplacement(value)); //Can't do this as contentAfterReplacement is passed on and later parsed as a regex (Ie, it can't have special characters escaped).
  • 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-09T18:04:55+00:00Added an answer on June 9, 2026 at 6:04 pm

    Why not use String#replace method instead of replaceAll. replaceAll uses regex but replace doesn’t use regex in replacement string.

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

Sidebar

Related Questions

I am doing some fairly extensive string manipulations using regular expressions in Java. Currently,
I'm working on code for a Java project, I currently have replaceAll(String, int) in
I currently have this code that creates 4 tabs using tabactivity: public class toknapp
I'm currently in an introductory level Java class, and am working on the classic
We're currently facing some issues during Unit Testing. Our class is multithreading some function
I am currently trying to make sure that a member function of one class
I'm currently writing a class to handle all database-activity in my application, and so
Consider the following problem: You have a class 'A' that serves as a base
I have this HTML escaping method: public static String stringToHTMLString(String string) { StringBuffer sb
Currently, I am writing a MiddleWare application that synchronizes information between and accounting application

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.