Skip to main content

Posts

Showing posts with the label matcher

Replacing $ in a String in Java

I faced an issue in my project recently where we read a password for an admin user first from a file which has a dummy or wrong password written in it. Then I read the password from database and replace the dummy password with the real password that I get from Database. So the code was something very simple like this (this is actually different from originalk). String myXmlDoc = readALargeXmlAsString(); String realPassword = readFromDatabase(); String password = myXmlDoc.replaceAll("dummyPassword", realPassword); All on a sudden we found that this replacement started failing after the password of that admin user was changed recently. Here is the exception log. Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference at java.util.regex.Matcher.appendReplacement(Matcher.java:706) at java.util.regex.Matcher.replaceAll(Matcher.java:806) at java.lang.String.replaceAll(String.java:2000) at com.salesforce.test.StringReplaceTest.mai...