gpt4 book ai didi

java - 使用正则表达式匹配目标文件夹中以扩展名 war 结尾的文件

转载 作者:行者123 更新时间:2023-11-29 05:05:34 25 4
gpt4 key购买 nike

我正在尝试使用正则表达式从目标文件夹中获取文件。我正在部署 war 文件,为了运行我的功能测试,它需要确定保存在目标文件夹中的 war 文件,以便它可以安装它并针对它运行我的 tomcat 实例。

我不能提到静态路径,因为我的 war 版本在发布后会改变。我不想每次都手动更新它。

我使用了以下正则表达式,但似乎不起作用。

Matcher matcher = Pattern.compile("(.+?)(\\.war)$").matcher("./target/");
webapp = new File(matcher.group(1));

我希望获取目标文件夹中存在的 war 文件。

我还可以将两个不同的匹配器 ("./target/"or ./nameOfComponent/target/") 附加到一个 Pattern 吗?

最佳答案

遍历文件,并检查每个文件的模式。

// only needed once for all files
Pattern pattern = Pattern.compile("(.+?)(\\.war)$");

// collect all files in all relevant directories
List<File> potentialFiles = new ArrayList<>();
potentialFiles.addAll(Arrays.asList(new File("./target/").listFiles()));
potentialFiles.addAll(Arrays.asList(new File("./nameOfComponent/target/").listFiles()));

File webapp = null;
for (File file : potentialFiles) {
Matcher matcher = pattern.matcher(file.getName());
if (matcher.matches()) {
webapp = file;
break; // use this line if you only want the first match
}
}

// use "webapp", but expect null if there was no match

关于java - 使用正则表达式匹配目标文件夹中以扩展名 war 结尾的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30511547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com