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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:44:58+00:00 2026-06-10T12:44:58+00:00

I am trying below code but getting error String x = aaa XXX bbb;

  • 0

I am trying below code but getting error

String x = "aaa XXX bbb";
    String replace = "XXX";
    String y = "xy$z";
    String z=y.replaceAll("$", "\\$");
    x = x.replaceFirst(replace, z);
    System.out.println(x);

Error

Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
    at java.util.regex.Matcher.appendReplacement(Unknown Source)
    at java.util.regex.Matcher.replaceFirst(Unknown Source)
    at java.lang.String.replaceFirst(Unknown Source)
    at Test.main(Test.java:10)

I want result as

aaa xy$z bbb
  • 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-10T12:44:59+00:00Added an answer on June 10, 2026 at 12:44 pm

    Use replace() instead, which doesn’t use regular expressions, since you don’t need them at all:

    String x = "aaa XXX bbb";
    String replace = "XXX";
    String y = "xy$z";
    x = x.replace(replace, y);
    System.out.println(x);
    

    This will print aaa xy$z bbb, as expected.

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

Sidebar

Related Questions

I'm trying to send a string via ftp using the code below but I'm
Below is the code I am trying to test, but getting null pointer exception
I am trying to port the code below to mootools 1.1 code but am
I am trying to run the code below but it keeps locking up my
I'm trying to find a subpalindrome in Java. But the below code doesn't provide
I'm trying to run the below-mentioned code in VB(Excel Macro) but I'm stuck with
I'm trying to set the port on a cookie in ASP.NET (code below), but
I'm trying to learn C++ currently, but I'm having issues with the code below.
I'm trying to flip between two views. That's easy, the code is below, but
I am trying to add a custom font but can't. I am getting error

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.