其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

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

GG网络技术分享 2025-03-18 16:15 1


问题描述:

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(\"/\");

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

网友观点:

     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

利用正则表达式进行字符串替换(replace方法)

语法

标签:

提交需求或反馈

Demand feedback