tfancy2012
2016-11-22 04:21
采纳率: 71.4%
浏览 1.2k

JAVA正则表达式替换,替换不成功

str = "D:\code\erp\target\class\artifacts\erp_war_exploded\/attachment/image/seal/4//5858.png"
我想把str的\/和//都替换成/
String REGEX = "\/|//";
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(str);
str=m.replaceAll("/");

这样写为什么不行??该怎么搞?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • qq_36784106 2016-11-22 07:33
    已采纳
         String str ="D:\\code\\erp\\target\\class\\artifacts\\erp_war_exploded\\/attachment/image/seal/4//5858.png";
         System.out.println(str);
            //   我想把str的\/和//都替换成/
        String REGEX = "\\\\/|//";
        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(str);
        str=m.replaceAll("/");
        System.out.println(str);
    
            运行结果:
            D:\code\erp\target\class\artifacts\erp_war_exploded\/attachment/image/seal/4//5858.png
      D:\code\erp\target\class\artifacts\erp_war_exploded/attachment/image/seal/4/5858.png
    
    打赏 评论

相关推荐 更多相似问题