简体   繁体   English

如何从Java中的字符串获取(拆分)文件名?

[英]How to get (split) filenames from string in java?

I have a string that contains file names like: 我有一个包含文件名的字符串,例如:

"file1.txt file2.jpg tricky file name.txt other tricky filenames containing áéíőéáóó.gif"

How can I get the file names, one by one? 如何获得一个一个的文件名? I am looking for the most safe most through method, preferably something java standard. 我正在寻找最安全,最通过的方法,最好是Java标准的方法。 There has got to be some regular expression already out there, I am counting on your experience. 已经有了一些正则表达式,我指望您的经验。

Edit: expected results: "file1.txt", "file2.jpg", "tricky file name.txt", "other tricky filenames containing áéíőéáóó.gif" 编辑:预期结果:“ file1.txt”,“ file2.jpg”,“ tricky文件名.txt”,“其他包含áéíőéáóó.gif的棘手文件名”

Thanks for the help, Sziro 感谢您的帮助,Sziro

If you want to use regular expressions you can find all the occurrences of: 如果要使用正则表达式,则可以找到所有出现的情况:

(\S.*?\.\S+)

(you can test it here ) (您可以在这里进行测试)

Regular expresion that enrico.bacis suggested (\\S. ?.\\S+)* will not work if there are filenames without characters before "." 如果文件名在“。”之前没有字符,则enrico.bacis建议的常规表达式(\\ S。 ?。\\ S +)*将不起作用。 like .project . .project一样。

Correct pattern would be: 正确的模式是:

(([^ .]+ +)*\S*\.\S+)

You can try it here . 您可以在这里尝试。

Java program that could extract filenames will look like: 可以提取文件名的Java程序如下所示:

String patternStr = "([^ .]+ +)*\\S*\\.\\S+";
String input = "file1.txt .project file2.jpg tricky file name.txt other tricky filenames containing áéíoéáóó.gif";
Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {            
    System.out.println(matcher.group());
}
    String txt = "file1.txt file2.jpg tricky file name.txt other tricky filenames containing áéíőéáóó.gif";
    Pattern pattern = Pattern.compile("\\S.*?\\.\\S+"); // Get regex from enrico.bacis
    Matcher matcher = pattern.matcher(txt);
    while (matcher.find()) {
        System.out.println(matcher.group().trim());
    }

If there are spaces in the file names, it makes it trickier. 如果文件名中有空格,则将变得更加棘手。

If you can assume there are no dots (.) in the file names, you can use the dot to find each individual records as has been suggested. 如果可以假设文件名中没有点(。),则可以按照建议使用点来查找每个单独的记录。

If you can't assume there are no dots in file names, eg my file.new something.txt 如果不能假设文件名中没有点,例如我的file.new something.txt

In this situation, I'd suggest you create a list of acceptable extentions, eg .doc, .jpg, .pdf etc. 在这种情况下,建议您创建一个可接受范围的列表,例如.doc,.jpg,.pdf等。

I know the list may be long, so it's not ideal. 我知道列表可能很长,因此并不理想。 Once you have done this you can look for these extensions and assume what's before it is a valid filename. 完成此操作后,您可以查找这些扩展名并假设它是有效的文件名之前的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM