1 回答

TA贡献1824条经验 获得超6个赞
你可以这样做(Java 9+):
String sample = "<DocumentImagePath>95230-88\\M0010002F.tif\\test</DocumentImagePath>\r\n" +
"95230-88\\M0010002F.tif\\test\r\n" +
"<DocumentImagePath>123-88\\M0010002F.tif\\test</DocumentImagePath>\r\n" +
"<DocumentImagePath>abc-88\\M0010002F.tif\\test</DocumentImagePath>\r\n";
String result = Pattern.compile("<DocumentImagePath>.*?</DocumentImagePath>")
.matcher(sample)
.replaceAll(r -> r.group().replace('\\', '/'));
System.out.println(result);
输出
<DocumentImagePath>95230-88/M0010002F.tif/test</DocumentImagePath>
95230-88\M0010002F.tif\test
<DocumentImagePath>123-88/M0010002F.tif/test</DocumentImagePath>
<DocumentImagePath>abc-88/M0010002F.tif/test</DocumentImagePath>
更新:对于 Java 8 及更早版本,请使用以下代码:
StringBuffer buf = new StringBuffer();
Matcher m = Pattern.compile("<DocumentImagePath>.*?</DocumentImagePath>").matcher(sample);
while (m.find())
m.appendReplacement(buf, m.group().replace('\\', '/'));
String result = m.appendTail(buf).toString();
添加回答
举报