3

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

user1769640
  • 41
  • 1
  • 2

3 Answers3

4

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

TNgo
  • 140
  • 5
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")?

Community
  • 1
  • 1
lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • Do you mean `file.separator` (/ or \, usually)? `path.separator` is the classpath separator and is `:` on Linux and `;` on Windows. – DNA Oct 23 '12 at 21:38
1

This should take care of fixing slashes:

String str = xml.replaceAll("\\\\|/", System.getProperty("file.separator"));
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thank you for the answer first! but this results an exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 1 – user1769640 Oct 23 '12 at 23:04
  • That should not happen, I had actually tested it before posting. See here a live demo of this code: http://ideone.com/TlzlRa – anubhava Oct 24 '12 at 04:37