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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:38:40+00:00 2026-05-15T00:38:40+00:00

In Java, and it seems in a few other languages, backreferences in the pattern

  • 0

In Java, and it seems in a few other languages, backreferences in the pattern are preceded by a backslash (e.g. \1, \2, \3, etc), but in a replacement string they preceded by a dollar sign (e.g. $1, $2, $3, and also $0).

Here’s a snippet to illustrate:

System.out.println(
    "left-right".replaceAll("(.*)-(.*)", "\\2-\\1") // WRONG!!!
); // prints "2-1"

System.out.println(
    "left-right".replaceAll("(.*)-(.*)", "$2-$1")   // CORRECT!
); // prints "right-left"

System.out.println(
    "You want million dollar?!?".replaceAll("(\\w*) dollar", "US\\$ $1")
); // prints "You want US$ million?!?"

System.out.println(
    "You want million dollar?!?".replaceAll("(\\w*) dollar", "US$ \\1")
); // throws IllegalArgumentException: Illegal group reference

Questions:

  • Is the use of $ for backreferences in replacement strings unique to Java? If not, what language started it? What flavors use it and what don’t?
  • Why is this a good idea? Why not stick to the same pattern syntax? Wouldn’t that lead to a more cohesive and an easier to learn language?
    • Wouldn’t the syntax be more streamlined if statements 1 and 4 in the above were the “correct” ones instead of 2 and 3?
  • 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-05-15T00:38:40+00:00Added an answer on May 15, 2026 at 12:38 am

    Is the use of $ for backreferences in replacement strings unique to Java?

    No. Perl uses it, and Perl certainly predates Java’s Pattern class. Java’s regex support is explicitly described in terms of Perl regexes.

    For example: http://perldoc.perl.org/perlrequick.html#Search-and-replace

    Why is this a good idea?

    Well obviously you don’t think it is a good idea! But one reason that it is a good idea is to make Java search/replace support (more) compatible with Perl’s.

    There is another possible reason why $ might have been viewed as a better choice than \. That is that \ has to be written as \\ in a Java String literal.

    But all of this is pure speculation. None of us were in the room when the design decisions were made. And ultimately it doesn’t really matter why they designed the replacement String syntax that way. The decisions have been made and set in concrete, and any further discussion is purely academic … unless you just happen to be designing a new language or a new regex library for Java.

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

Sidebar

Ask A Question

Stats

  • Questions 503k
  • Answers 503k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try using mousePressed instead of mouseClicked. mouseClicked looks for multiple… May 16, 2026 at 2:56 pm
  • Editorial Team
    Editorial Team added an answer If you have it in an arraylist, you could do… May 16, 2026 at 2:56 pm
  • Editorial Team
    Editorial Team added an answer It turns out that boldfacing is hard-coded in several places,… May 16, 2026 at 2:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

It seems to me that, in some ways, Java is where C was a
Let me first say that I have quite a lot of Java experience, but
I'm probably going to take some heat for this question. But I'd like to
Is for keyword obsolete or may become obsolete just as goto in languages like
Is there anything simple Java can't do that can be done in a similar
so I asked here few days ago about C# and its principles. Now, if
I asked a question a few days ago ( Access to SQL Server 2005
I'm using com.sun.net.httpserver.HttpServer in my project. However, it seems that the server leaks connections
I am writing JVMTI code to profile Java programs, which mostly entails obtaining stack
I export try.jar file using 32 bit java libraries. On the client site, I

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.