问题
I'm not very sure there is any regex to replace thoese things:
This is a string value read from a xml file saved through Linux machine
<pcs:message schema="models/HL7_2.5.model"/>
and this is the one saved in Windows machine
<pcs:message schema="model\HL7_2.5.model"/>
This is why the file getting an error in eclipse while exported in Linux and imported in Windows or vise versa.
Is there any regex to find and replace the value(slash and back slash) within String? (not XML parsing) based on working OS?
Thanks in advance
回答1:
str = str.replaceAll("\\\\|/", "\\"+System.getProperty("file.separator"))
回答2:
Use the "file.separator" system property and base your regexp on that.
http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
Also see this: File.separator vs FileSystem.getSeparator() vs System.getProperty("file.separator")?
回答3:
This should take care of fixing slashes:
String str = xml.replaceAll("\\\\|/", System.getProperty("file.separator"));
来源:https://stackoverflow.com/questions/13039645/java-regex-to-replace-file-path-based-on-os