Java regex to replace OS based file path

I'm not very sure that there is any regex to replace thoese things:

This is a string value read from an xml file stored on a Linux computer.

<pcs:message schema="models/HL7_2.5.model"/>

and this is the one that is saved on the windows computer

<pcs:message schema="model\HL7_2.5.model"/>

This is why the file gets an error in eclipse when exporting to Linux and is imported to Windows or vice versa.

Is there any regular expression for finding and replacing a value (slash and backslash) inside a String? (not XML parsing) based on a working OS?

Thanks in advance

+3
source share
3 answers

str = str.replaceAll("\\\\|/", "\\"+System.getProperty("file.separator"))

+4
source

This should take care of fixing slashes:

String str = xml.replaceAll("\\\\|/", System.getProperty("file.separator"));
+1
source

All Articles