(转)正则表达式:string.replaceAll()中的特殊字符($ )与matcher.appendReplacement

string.replaceAll中的特殊字符

string.replaceAll(String regex, String replacement)中的replacement参数即替换内容中含有特殊字符 $ 时,需转义。

Java代码  收藏代码
  1. /* 
  2.  * 字符串"$ "中的$与字符互换位置 
  3.  */  
  4. public class SpecialCharReplace {  
  5.     public static void main(String[] args) {  
  6.         String str = "$ \";  
  7.         /* 
  8.          * string.replaceAll()中的特殊字符 $ 与   
  9.          *  
  10.          * 由于 $ 字符在作为替换内容时,是一个特殊字符,指反向引用前面的分组内容,所以把 
  11.          * 某字符替换成 $ 字符时,因该在前面加上转义字符 。 
  12.          *  字符就不用说了,本身就是转义字符,但为什么在作为替换内容时要使用四个  字符 
  13.          * ,这里又不是用在正则表达式里?这就是因为  字符在作为替换内容里也是一个特殊字 
  14.          * 符,它用来将前面讲的 $ 字符进行转换的,所以也为特殊字符。以下是replaceAll的 
  15.          * 源码片断,从源码就可以看出 $ 是两个特殊字符 
  16.          *  
  17.          * if (nextChar == '\') { 
  18.          *      cursor++; 
  19.          *      nextChar = replacement.charAt(cursor); 
  20.          *      result.append(nextChar); 
  21.          *      cursor++; 
  22.          * } else if (nextChar == '$') { 
  23.          *      // Skip past $ 
  24.          *      cursor++; 
  25.          *      ... 
  26.          * }else { 
  27.          *      result.append(nextChar); 
  28.          *      cursor++; 
  29.          * } 
  30.          */  
  31.         System.out.println(str.replaceAll("\$(\W)\\", "\\$1\$"));//  $  
  32.     }  
  33.   
  34. }   

Matcher对象的appendReplacement典型应用与特殊字符&的进一步分析

问题的提出 

字符串模板: 
    String template="尊敬的客户${customerName}你好!本次消费金额${amount},您帐户${accountNumber}上的余额为${balance},欢迎下次光临!"; 
其中以 ${ 开始 } 结尾的为待替换的变量域。 
数据存放于Map中,key为域名,value为域值。如: 
Map-- 
    customerName = 刘明 
    accountNumber = 888888888
    balance = $1000000.00
    amount = $1000.00 
请编写函数: 
    public static String composeMessage(String template, Map data) throw Exception 
实现将任意模板字符串中的变量域,按域名替换为data中的域值。 
例如,上例替换结果为: 
    "尊敬的客户刘明你好!本次消费金额$1000.00,您帐户888888888上的余额为$1000000.00,欢迎下次光临!" 
注:如果Map中找不到域值,以空字符串""替换。 

问题的解决

Java代码  收藏代码
    1. public class RegexExam {  
    2.     public static void main(String args[]) {  
    3.         HashMap data = new HashMap();  
    4.         String template = "尊敬的客户${customerName}你好!本次消费金额${amount},"  
    5.                 + "您帐户${accountNumber}上的余额为${balance},欢迎下次光临!";  
    6.         data.put("customerName", "刘明");  
    7.         data.put("accountNumber", "888888888");  
    8.         data.put("balance", "$1000000.00");  
    9.         data.put("amount", "$1000.00");  
    10.         try {  
    11.             System.out.println(composeMessage(template, data));  
    12.         } catch (Exception e) {  
    13.             e.printStackTrace();  
    14.         }  
    15.     }  
    16.   
    17.     public static String composeMessage(String template, Map data)  
    18.             throws Exception {  
    19.         String regex = "\$\{(.+?)\}";  
    20.         Pattern pattern = Pattern.compile(regex);  
    21.         Matcher matcher = pattern.matcher(template);  
    22.         /* 
    23.          * sb用来存储替换过的内容,它会把多次处理过的字符串按源字符串序 
    24.          * 存储起来。 
    25.          */  
    26.         StringBuffer sb = new StringBuffer();  
    27.         while (matcher.find()) {  
    28.             String name = matcher.group(1);//键名  
    29.             String value = (String) data.get(name);//键值  
    30.             if (value == null) {  
    31.                 value = "";  
    32.             } else {  
    33.                 /* 
    34.                  * 由于$出现在replacement中时,表示对捕获组的反向引用,所以要对上面替换内容 
    35.                  * 中的 $ 进行替换,让它们变成 "$1000.00" 或 "$1000000000.00" ,这样 
    36.                  * 在下面使用 matcher.appendReplacement(sb, value) 进行替换时就不会把 
    37.                  * $1 看成是对组的反向引用了,否则会使用子匹配项值amount 或 balance替换 $1 
    38.                  * ,最后会得到错误结果: 
    39.                  * 
    40.                  * 尊敬的客户刘明你好!本次消费金额amount000.00,您帐户888888888上的余额 
    41.                  * 为balance000000.00,欢迎下次光临! 
    42.                  * 
    43.                  * 要把 $ 替换成 $ ,则要使用 \\\& 来替换,因为一个  要使用 \ 来进 
    44.                  * 行替换,而一个 $ 要使用 \$ 来进行替换,因  与  $ 在作为替换内容时都属于 
    45.                  * 特殊字符:$ 字符表示反向引用组,而  字符又是用来转义 $ 字符的。 
    46.                  */  
    47.                 value = value.replaceAll("\$", "\\\$");  
    48.                 //System.out.println("value=" + value);  
    49.             }  
    50.             /* 
    51.              * 经过上面的替换操作,现在的 value 中含有 $ 特殊字符的内容被换成了"$1000.00" 
    52.              * 或 "$1000000000.00" 了,最后得到下正确的结果: 
    53.              * 
    54.              * 尊敬的客户刘明你好!本次消费金额$1000.00,您帐户888888888上的 
    55.              * 余额为$1000000.00,欢迎下次光临! 
    56.              * 
    57.              * 另外,我们在这里使用Matcher对象的appendReplacement()方法来进行替换操作,而 
    58.              * 不是使用String对象的replaceAll()或replaceFirst()方法来进行替换操作,因为 
    59.              * 它们都能只能进行一次性简单的替换操作,而且只能替换成一样的内容,而这里则是要求每 
    60.              * 一个匹配式的替换值都不同,所以就只能在循环里使用appendReplacement方式来进行逐 
    61.              * 个替换了。 
    62.              */  
    63.             matcher.appendReplacement(sb, value);  
    64.             System.out.println("sb = " + sb.toString());  
    65.         }  
    66.         //最后还得要把尾串接到已替换的内容后面去,这里尾串为“,欢迎下次光临!”  
    67.         matcher.appendTail(sb);  
    68.         return sb.toString();  
    69.     }  
    70. }  
原文地址:https://www.cnblogs.com/sunshisonghit/p/7218614.html