Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从文件URL获取路径?_Java_Regex_Indexing_Substring - Fatal编程技术网

Java 如何从文件URL获取路径?

Java 如何从文件URL获取路径?,java,regex,indexing,substring,Java,Regex,Indexing,Substring,我有以下格式的字符串: file://c:/Users/.... file://E:/Windows/.... file:///f:/temp/.... file:///H:/something/.... 我如何才能只获取c:/Users/..或H:/something/..?您可以将字符串中的字符串“file://”替换为空: String path = yourString.replace("file://", ""); 那怎么办 String path = yourString.rep

我有以下格式的字符串:

file://c:/Users/....
file://E:/Windows/....
file:///f:/temp/....
file:///H:/something/....

我如何才能只获取
c:/Users/..
H:/something/..

您可以将字符串中的字符串“file://”替换为空:

String path = yourString.replace("file://", "");
那怎么办

String path = yourString.replaceFirst("file:[/]*", "");

已测试并将替换任意数量的斜杠

String path = yourString.replaceFirst("file:/*", "");
如果你只想让它匹配两个或三个斜杠

String path = yourString.replaceFirst("file:/{2,3}", "");

如果它包含3个斜杠,比如
file://
,会怎么样?上面的答案不会满足您的要求file:///f:/temp/.... file:///H:/something/....
String path=yourString.replaceFirst(“^file:///?”,”)-没有方法
String。替换(String,String)
@christoff-Hammarström:那
file://c/Users
[]
是冗余的。而且,
//?
/{2,3}
更短、更简单。
String path = new java.net.URI(fileUrl).getPath();