Java regex to replace file path based on OS

一个人想着一个人 提交于 2019-12-30 10:57:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!