Swap String content for "$"

Asked

Viewed 192 times

0

I have a string with a content and I need to make a replaceAll, but the text to be replaced contains a "$" dollar sign and this causes the error Illegal group reference.

Example:

    String texto="teste {{texto}} teste";
    String trocar="_$_";
    texto=texto.replaceAll("\\{\\{texto\\}\\}", trocar);

Note: This "swap" string comes from a database, so I cannot add the . The example is only illustrative.

  • Tried to use the scape? ""?

  • This "swap" string comes from a database. The example is only illustrative. (editing the pargunta)

  • You can force the escape of $ for \$, edited my answer to exemplify

3 answers

2

Missed using the exhaust \

public static void main(String[] args) {
    String texto = "teste {{texto}} teste";
    String trocar = "_\\$_";
    texto = texto.replaceAll("\\{\\{texto\\}\\}", trocar);
    System.out.println(texto);
}

you can still use the quote to facilitate the search:

String buscar = Pattern.quote("{{texto}}");
texto = texto.replaceAll(buscar, trocar);

now the value coming from another source, you can solve by trying to make the escape of $ for \$ a way to do this:

public static void main(String[] args) {
    String texto = "teste {{texto}} teste";
    String origem = "_$_";
    String trocar = origem.replaceAll(Pattern.quote("$"), "\\\\\\$");
    String buscar = Pattern.quote("{{texto}}");
    texto = texto.replaceAll(buscar, trocar);
    System.out.println(texto);
}
  • edited the question... the String swap comes from a database, so I cannot add the \.

  • edited the answer to exemplify

  • Show! It worked, but you can explain to me why so many bars \ ?

  • yes, actually the symbol $ in regular expression means grouping you can usually use $1. $2 for group 1 group 2 ... within a regular expression. ai in the case of java uses as escape, this way you need to escape the $ to stay \$ more to escape the you need to escape the so at the end you will have an escape for each special character that will be used

2

String.replaceAll takes a standard regular expression matching as its first parameter, and a standard regular expression substitution as its second parameter - and $has a specific meaning in regular expressions (in both matching patterns and substitution patterns, although in different directions).

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

  • You’re right. Using replace works. I was using replaceAll because the "text" string can contain several occurrences of {{text}} and I thought that using replace only the first occurrence would be replaced.

0

Use the scape:

public static void main(String[] args) {
        String texto="teste {{texto}} teste";
        String trocar="_\\$_";
        texto=texto.replaceAll("\\{\\{texto\\}\\}", trocar);
        System.out.println(texto);

    }
  • I edited the question... the swap string comes from a database, so I can’t add the \.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.