[英]Get Filname without extension from string with regex
I have a string that contains a URL to a file. 我有一个包含文件URL的字符串。 From that string I like to get just the filename without extension.
从那个字符串我喜欢只获得没有扩展名的文件名。
For example: 例如:
http://path/Lists/Test/Attachments/1/Document Test.docx
From that example I like to get back: Document Test
从那个例子我喜欢回来:
Document Test
I already have the following pattern: 我已经有了以下模式:
(?<=\/)(\w+)(?=\.\w+(\?.*)*$)
But it does not work if the filename contains spaces... How can I change this pattern that it is more flexible? 但是如果文件名包含空格则不起作用...如何更改此模式更灵活?
要仅在扩展名之前捕获字符(不限制文件名可能包含的字符),请使用以下命令:
/[^/]*(?=\.[^.]+($|\?))/
Don't use regex when you don't have to. 不必使用正则表达式。 In this case substring between
lastIndexOF
/
and .
在这种情况下,
lastIndexOF
/
和之间的子串.
would give you what you want. 会给你你想要的东西。
String data = "Document Testdocx";
int start = data.lastIndexOf('/')+1;
int end = data.lastIndexOf('.');
if (end == -1) end = data.length();
System.out.println(data.substring(start , end));
But if you really must use regex you can try this pattern: (?<=/|^)[^./]+(?=\\\\.\\\\w+$|$)
但如果你真的必须使用正则表达式,你可以尝试这种模式:
(?<=/|^)[^./]+(?=\\\\.\\\\w+$|$)
try 尝试
String s = "http://path/Lists/Test/Attachments/1/Document Test.docx";
s = s.replaceAll(".+/(.+)\\..+", "$1");
System.out.println(s);
output 产量
Document Test
([^?]+)\/([^/?]+)(\.[^.\?]+)(\?.*|)$
Even if the URL looks like 即使URL看起来像
http://example.com/foo/bar/baz blah.html?params=true
this can find the file name (without the directory) and the extension. 这可以找到文件名(没有目录)和扩展名。
Probably better would be to parse the URL with java.net.URL , and use URL.getPath(). 可能更好的方法是使用java.net.URL解析URL,并使用URL.getPath()。
而不是(?<=/)(\\w+)(?=.\\w+(\\?.)$)
,请尝试(.+?)(\\.[^.]*$|$)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.