0

Using replaceAll() is giving me a rexex exception.

This is the code I am using:

public class test {

    public static void main(String[] args) {
        String text= "This is to be replaced &1 ";
        text = text.replaceAll("&1", "&");
        System.out.println(text);   
    }
}

EXCEPTION:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
    at java.util.regex.Matcher.appendReplacement(Unknown Source)
    at java.util.regex.Matcher.replaceAll(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)
    at test.main(test.java:7)
Reese Moore
  • 11,524
  • 3
  • 24
  • 32
tasa
  • 1
  • 1
  • 1

6 Answers6

4

Seems to work fine for me. http://ideone.com/7qR6Z

But for something this simple, you can avoid regex and just use string.replace()

text = text.replace("&1", "&");
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • Thanks a lot guys, that was a simple misconception i had. – tasa May 27 '11 at 18:49
  • `String.replace()` takes only char as parameters. http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html – Marcelo May 27 '11 at 22:10
  • @Marcelo there is another that takes charsequence which is string. – Bala R May 28 '11 at 00:21
  • @Bala R Can't find it in the javadoc at all :s – Marcelo May 28 '11 at 05:42
  • @Marcelo Your javadoc link is for 1.4.2. it's listed in javaoc for 1.5. http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html not sure why it's not listed in 1.6 but it's definitely there. ( besides they will not remove a method just like that; they will deprecate it). – Bala R May 28 '11 at 11:00
  • @Bala R Thank you for the link. – Marcelo May 28 '11 at 13:05
2

If you don't want regex then use String#replace method instead like this:

"This is to be replaced &1 ".replace("&1", "&")
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

My solution for this error while replacing with "$" sign was to replace all "$" with "\\$" like in code bellow:

myString.replaceAll("\\$", "\\\\\\$");
Miroslav
  • 322
  • 5
  • 13
1

You can use Pattern.quote() to compile any string into a regular expression. Try:

public class test {
   public static void main(String[] args) {
        String text= "This is to be replaced &1 ";
        text = text.replaceAll(Pattern.quote("&1"), "&");
        System.out.println(text);   
    }
}
Marcelo
  • 11,218
  • 1
  • 37
  • 51
0

As it stands, your code works fine. However, if you mistyped or something and actually have

text = text.replaceAll("&1", "$");

Then you DO have to escape the replacement:

text = text.replaceAll("&1", "\\$");
josh.trow
  • 4,861
  • 20
  • 31
0

Your question title shows how do i replace any string with a “$ ” in java? but your question text says String text= "This is to be replaced &1 "

If you're actually trying to replace a dollar sign, this is a special character in regular expressions, you need to escape it with a backslash. You need to escape that backslash, because blackslash is a special character in Java, so assuming dollar sign is what you intended:

String text = "This is to be replaced $1 ";

text = text.replaceAll("\\$1", "\\$");

System.out.println(text);

EDIT: Clarify some text

Taylor
  • 3,942
  • 2
  • 20
  • 33