[英]Groovy - How to split a String containing a number of FileNames but some of them contains spaces?
How to split a string containing a number of file names (including names containing spaces)? 如何分割包含多个文件名(包括空格的名称)的字符串?
Example String: 示例字符串:
randomFolderNameA/path/to/file1.java randomFolderNameB/path/to/file2.sql randomFolderNameC/path/to/file3 with space.xml file4 with space.sql
Expected output: 预期产量:
randomFolderNameA/path/to/file1.java
randomFolderNameB/path/to/file2.sql
randomFolderNameC/path/to/file3 with space.xml
file4 with space.sql
Assuming all your paths are absolute, you can use look-ahead assertion in your regex: 假设所有路径都是绝对路径,则可以在正则表达式中使用前瞻性断言:
def text = "/path/to/file1.java /path/to/file2.sql /path/to/file3 with space.xml"
println text.split(" (?=/)")
And that outputs [/path/to/file1.java, /path/to/file2.sql, /path/to/file3 with space.xml]
然后输出
[/path/to/file1.java, /path/to/file2.sql, /path/to/file3 with space.xml]
Taht regex splits the string on a space followed by a /
Taht正则表达式将字符串拆分为空格,后跟
/
EDIT: For the updated example, it's possible to look at extensions in file names, although you'd need to carefully consider what your input can include: 编辑:对于更新的示例,可以查看文件名中的扩展名,尽管您需要仔细考虑您的输入内容可以包括:
def text = "randomFolderNameA/path/to/file1.java randomFolderNameB/path/to/file2.sql randomFolderNameC/path/to/file3 with space.xml file4 with space.sql"
println text.split("(?<=\\.[a-zA-Z0-9]{1,4}) ")
That outputs the following, as expected: 如预期的那样输出以下内容:
[randomFolderNameA/path/to/file1.java,
randomFolderNameB/path/to/file2.sql,
randomFolderNameC/path/to/file3 with space.xml,
file4 with space.sql]
But as noted above, you need to be sure that paths only include file names and the extension regex is valid (I used "a dot followed by 1 to 4 alpha-numeric characters") 但是如上所述,您需要确保路径仅包含文件名并且扩展名regex有效(我使用了“点后跟1至4个字母数字字符”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.