首页 > 技术文章 > 特殊字符导致用正则表达式进行字符串替换失败,Java replaceAll()方法报错Illegal group reference

xunux 2015-10-05 16:09 原文

 

 

String str = "给商品||?>\\n阳澄湖大闸蟹!@#$%^&*()_+-=?:\",.]\\|~.,\/??\\\\|\\br点赞"
1 String tradeDescription = "||?>\\n阳澄湖大闸蟹!@#$%^";
2 String description = "给商品 #goodsName# 点赞";
3 String result = description.replaceAll("#goodsName#", Matcher.quoteReplacement(CommonUtil.toString(tradeDescription)));

 

 

 

参考:

 

http://www.colabug.com/thread-1131948-1-1.html

推荐阅读