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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:01:09+00:00 2026-06-16T17:01:09+00:00

I wrote this code: public static void main(String args[]) throws Exception { String template

  • 0

I wrote this code:

public static void main(String args[]) throws Exception {
    String template = "The user has spent amount in a day";
    String pattern = "amount";
    String output = template.replaceAll(pattern, "$ 100");
    System.out.println(output);
}

This is what happens when I run it:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
        at java.util.regex.Matcher.appendReplacement(Matcher.java:713)
        at java.util.regex.Matcher.replaceAll(Matcher.java:813)
        at java.lang.String.replaceAll(String.java:2190)
        at demo.BugDemo.main(BugDemo.java:16)
Java Result: 1

I’m reading data from a file. Should I escape all $ symbols in the file data, or is this an unnecessary process? Is there any other class or library to handle this situation?

What is the problem with having a special symbol in the replacement text (not in the regex)?

Notes:

  • I don’t want to check every character to escape. That’s why I am asking this question.

  • I’m using Java 6.

  • 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-16T17:01:10+00:00Added an answer on June 16, 2026 at 5:01 pm

    String.replaceAll takes a regular expression matching pattern as its first parameter, and a regular expression replacement pattern as its second parameter – and $ has a specific meaning in regular expressions (in both matching patterns and replacement patterns, although in different senses).

    Just use String.replace instead, and I suspect all your problems will go away. You should only use replaceAll when you genuinely want to match/replace via a regular expression – which I don’t think you do in this case.

    EDIT: As to your question:

    What is the problem with having a special symbol in the replacement text (not in the regex)?

    Again, the documentation for replaceAll makes this clear:

    Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.

    So if you want to treat the matching pattern as a regular expression, but not the replacement, then use Matcher.quoteReplacement.

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

Sidebar

Related Questions

I took this code : 28 public static void main(String[] args) throws IOException {
I have the following code: public static void main(String[] args) throws Exception { String
I wrote this code for converting binary to text . public static void main(String
public static void main(String args[]) throws IOException { Process p = Runtime.getRuntime().exec(java E:/workspace/JNIProgram/src/JNIProgram.class); }
I am using this code to write asynchronously to a file public static void
Okay, so i have this code i wrote: class Connection { public static StreamWriter
I wrote this code in C# to encrypt a string with a key: private
Is it worth to write this piece of code: RelayCommand _saveCommand; public ICommand SaveCommand
I wrote this code for zoom in/out . it works but even with one
I wrote this code for zoom in / out and it suppesed to only

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.