Thursday, April 9, 2009

JAVA: illegal group reference

How:
You have a string "I like dollar symbol".
and you want to replace the string with "I like $ symbol".
Code:

String a = "I like dollar symbol"
String replacement = "$"
a = a.replaceAll("dollar", replacement);


This will throw an exception "Illegal group reference."

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)


Solution:
Here the problem is "$" is a Metacharacter which should be escaped.

  • replacement = Matcher.quoteReplacement(replacement);
    a = a.replaceAll("dollar", replacement);


    About quoteReplacement method in Matcher class
    Ref. link: Click here
    Returns a literal replacement String for the specified String. This method produces a String that will work use as a literal replacement s in the appendReplacement method of the Matcher class. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes ('\') and dollar signs ('$') will be given no special meaning.

  • put two backslashes before "$".

    String replacement = "\\$"



21 comments:

  1. Hi,The key goal when designing websites is to form the ideal site with Web Design Cochin for each individual. My in depth background online and over twenty-five years of programming helps tremendously.Thanks.....

    ReplyDelete
  2. Nice..I ran into this issue with unit testing!

    ReplyDelete
  3. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    Data Science training in Chennai | Data Science Training Institute in Chennai
    Data science online training | online Data Science certification Training-Gangboard

    ReplyDelete
  4. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
    Data Science training in Chennai | Data Science Training Institute in Chennai
    Data science online training | online Data Science certification Training-Gangboard

    ReplyDelete
  5. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
    Java training in Chennai

    Java training in Bangalore

    ReplyDelete
  6. Really you have done great job,There are may person searching about that now they will find enough resources by your post
    Best Devops Training in pune
    Devops Training in Bangalore
    Microsoft azure training in Bangalore
    Power bi training in Chennai

    ReplyDelete
  7. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing..
    Believe me I did wrote an post about tutorials for beginners with reference of your blog. 




    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  8. Really very happy to say, your post is very interesting to read. I never stop myself to say something about it. You’re doing a great job. Keep it up…

    Start your journey with Best SAP Course and get hands-on Experience with 100% Placement assistance from experts Trainers @Softgen Infotech Located in BTM Layout Bangalore. Expert Trainers with 8+ Years of experience, Free Demo Classes Conducted.

    ReplyDelete