replaceAll with metacharacter
[nobr]Hi all
I need to replace a String contains $ sign with another String contains $sign. Since $ sign have special meaning and need to add metacharacter between the message. I have some problem when the String used for replacement have $ sign. A exception generated
java.lang.IllegalArgumentException: Illegal group reference
Here are my coding
import java.util.regex.Pattern;
import java.util.regex.Matcher;
publicfinalclass ReplaceTest{
privatestatic String REGEX ="\\Q$person.firstName $person.lastName<br/>\\E";
privatestatic String INPUT =" #foreach($person in $personList)<br />$person.firstName $person.lastName<br/>#end
privatestatic String REPLACE ="\\$person.firstName1 \\$person.lastName1<br/>";
privatestatic String REPLACE1 ="\\Q$person.firstName1 $person.lastName1<br/>\\E";
publicstaticvoid main(String[] argv){
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT);
INPUT = m.replaceAll(REPLACE);
System.out.println(INPUT);
}
}
using REPLACE without problem but not REPLACE1. Since this String will read from file rather than input at coding level, I cannot add backslash in front of every $. Any solution for this? Thanks.
regards
wayne[/nobr]

