Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class Regexexam {
public static void Main (String args[]) {
HashMap data = new HashMap ();
String template = "Dear customer ${customername} Hello!" This consumption amount ${amount}, "
+ "The balance on your account ${accountnumber} is ${balance}, welcome to the next visit!" ";
Data.put ("CustomerName", "Liu Ming");
Data.put ("AccountNumber", "888888888");
Data.put ("Balance", "\\$1000000.00");
Data.put ("Amount", "\\$1000.00");
try {
Dear Customer, Liu Ming Hello! This consumption amount $1000.00, the balance on your account 888888888 is $1000000.00, welcome to visit next time!
System.out.println (composemessage (template, data));
} catch (Exception e) {
E.printstacktrace ();
}
}
public static string Composemessage (string template, Map data)
Throws Exception {
String regex = "\\$\\{(. +?) \\}";
Pattern pattern = pattern.compile (regex);
Matcher Matcher = pattern.matcher (template);
/*
* SB is used to store the replaced content, it will be processed several times the string by the source string sequence
* Store it up.
*/
StringBuffer sb = new StringBuffer ();
while (Matcher.find ()) {
String name = Matcher.group (1);//Key Name
String value = (string) data.get (name);//Key value
if (value = = null) {
Value = "";
} else {
/*
* Since $ appears in replacement, it represents a reverse reference to the capturing group, so replace the above
* Replace the $ in to make them "\$1000.00" or "\$1000000000.00" so
* When replacing with matcher.appendreplacement (SB, value) below, the
* $ $ is treated as a reverse reference to a group, otherwise a sub-match value of amount or balance will be used to replace the
*, you will get the wrong result at the end:
*
* Dear customer, Liu Ming Hello! The amount amount000.00, the balance on your account 888888888
* For balance000000.00, welcome to the next visit!
*
* To replace $ to \$, replace with \\\\\\&, because a \ To use \\\ to enter
* Line substitution, and a $ to be replaced with \\$, since \ and $ are part of the replacement content
* Special characters: The $ character represents a reverse reference group, while the \ character is used to escape the $ character.
*/
Value = Value.replaceall ("\\$", "\\\\\\$");
System.out.println ("value=" + value);
}
/*
* After the above substitution, the contents of the current value containing $ special characters are replaced with "\$1000.00"
* or "\$1000000000.00", and finally get the right result:
*
* Dear customer, Liu Ming Hello! This consumption amount $1000.00, your account 888888888
* The balance is $1000000.00, welcome to the next visit!
*
* In addition, here we use the Appendreplacement () method of the Matcher object for the substitution operation, and
* Do not use the ReplaceAll () or Replacefirst () method of the string object for the substitution operation because
* They can only be used for a one-time simple replacement operation, and can only be replaced by the same content, and here is required every
* The substitution value of a match is different, so it is only possible to use the Appendreplacement method in the loop to
* One replaced.
*/
Matcher.appendreplacement (SB, value);
System.out.println ("SB =" + sb.tostring ());
}
Finally, you have to end the string to the replacement of the contents of the back, where the end of the string ", welcome to the next visit! ”
Matcher.appendtail (SB);
return sb.tostring ();
}
}
class T1 {
public static void Main (String a[]) {
String s = "e:$\\jbx\\$\\x9\\\\io9";
S.replace ("\ \", "\\\\");
System.out.println ("s=" + s);
String SS;
String SSS;
The path s in the ' \ ' for ' \ \ ', why the whole 8 ' \ '? I thought 4 of them would be fine.
SS = S.replaceall ("\\\\", "\\\\\\\\");
String AA = "E: $jbx $x9\\io9";
SSS = Ss.replaceall ("\\$", "\\\\\\$");
System.out.println (Ss.replaceall ("\\$", "\\\\\\$"));
System.out.println ("s=" + s);
System.out.println ("ss=" + ss);
System.out.println ("sss=" + sss);
Output
s= E:\jbx\x9\io9
Ss=e:\\jbx\\x9\\io9
}
}
Import Java.util.regex.Pattern;
public class Test {
public static void Main (string[] args) {
String s = "SELECT Top ${fetchnum} * from TBL";
String escaped = Pattern.quote ("${fetchnum}"); = "\q${fetchnum}\e"
String replaced = S.replaceall (escaped, "123");
SYSTEM.OUT.PRINTLN (replaced);
}
}
JAVA String ReplaceAll