我从正确的答案中得到了斜杠和反斜杠的正则表达式:正则表达式匹配JAVA中的斜杠
String path = "C:\\system/properties\\\\all//";
String replaced = path.replaceAll("[/\\\\]+",
System.getProperty("file.separator"));
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误:
线程"main"中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1
这个正则表达式有什么问题?删除+不会改变任何东西,错误信息是相同的......
ass*_*ias 12
它记录在Javadoc中:
请注意,替换字符串中的反斜杠(\)和美元符号($)可能会导致结果与将其视为文字替换字符串时的结果不同; 看
Matcher.replaceAll.使用Matcher.quoteReplacement(java.lang.String)抑制这些字符的特殊含义,如果需要的话.
所以你可以尝试这个:
String replaced = path.replaceAll("[/\\\\]+", Matcher.quoteReplacement(System.
getProperty("file.separator")));
Run Code Online (Sandbox Code Playgroud)