I have the next string:
String var = "Hello NAME_USER, welcome to NAME_USER, your name is NAME_USER";
I want to replace all occurrences of NAME_USER
, the problem is when NAME_USER
has special characters (ex: !"#$%&/()=)(/&%
), a java.lang.IllegalArgumentException
is thrown.
The complete code:
String var = "Hello NAME_USER, welcome to NAME_USER, your name is NAME_USER";
var = var.replaceAll("NAME_USER","!#$%&/()=)(/&%");
The exception:
java.lang.IllegalArgumentException: Illegal group reference
at java.util.regex.Matcher.appendReplacement(Matcher.java:857)
at java.util.regex.Matcher.replaceAll(Matcher.java:955)
at java.lang.String.replaceAll(String.java:2210)
at com.vupc.colegios.infraestructura.utilitarios.UtilitarioPlantilla.reemplazarTexto(UtilitarioPlantilla.java:119)
Note: NAME_USER
can be replace by any String
.
Is there any way to solve this?