Regex匹配2个字符串中的所有事件+;JAVA

Regex匹配2个字符串中的所有事件+;JAVA,java,regex,Java,Regex,我需要使用java在两个特定单词之间匹配反斜杠\并将其替换为正斜杠/。我尝试了这个,它在regex tester中运行良好,但在基于java的应用程序中测试时不起作用;获取此错误 org.apache.oro.text.regex.MalformedPatternException:Sequence(?您可以这样做(Java 9+): String sample=“95230-88\\M0010002F.tif\\test\r\n”+ “95230-88\\M0010002F.tif\\test

我需要使用java在两个特定单词之间匹配反斜杠\并将其替换为正斜杠/。我尝试了这个,它在regex tester中运行良好,但在基于java的应用程序中测试时不起作用;获取此错误

org.apache.oro.text.regex.MalformedPatternException:Sequence(?您可以这样做(Java 9+):

String sample=“95230-88\\M0010002F.tif\\test\r\n”+
“95230-88\\M0010002F.tif\\test\r\n”+
“123-88\\M0010002F.tif\\test\r\n”+
“abc-88\\M0010002F.tif\\test\r\n”;
字符串结果=模式。编译(“”?)
.matcher(样本)
.replaceAll(r->r.group().replace('\\','/');
系统输出打印项次(结果);
输出

95230-88/M0010002F.tif/test
95230-88\M0010002F.tif\test
123-88/M0010002F.tif/test
abc-88/M0010002F.tif/test
更新:对于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();
StringBuffer buf=new-StringBuffer();
Matcher m=Pattern.compile(“.*”).Matcher(示例);
while(m.find())
m、 appendReplacement(buf,m.group().replace('\\','/');
字符串结果=m.appendTail(buf).toString();

如果您发布您正在使用的Java代码,这会有所帮助。我不确定您如何匹配每行上的所有反斜杠。我的方法是使用正则表达式匹配开始标记和结束标记之间的所有文本,然后使用String.replaceAll将反斜杠替换为该字符串中的正向斜杠。谢谢您的快速响应。我看到了ng这个错误“这个表达式的目标类型必须是函数接口”,在lambda表达式中不是很好,我自己用Java8来解决。
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();