gpt4 book ai didi

java - 如何使用Java获取带有模式的子字符串

转载 作者:行者123 更新时间:2023-12-01 11:36:28 25 4
gpt4 key购买 nike

我有一个包含以下记录的文件:

drwxr-xr-x   - root supergroup          0 2015-04-05 05:26 /user/root
drwxr-xr-x - hadoop supergroup 0 2014-11-05 11:56 /user/root/input
drwxr-xr-x - hadoop supergroup 0 2014-11-05 03:06 /user/root/input/foo
drwxr-xr-x - hadoop supergroup 0 2015-04-28 03:06 /user/root/input/foo/bar
drwxr-xr-x - hadoop supergroup 0 2013-11-06 15:54 /user/root/input/foo/bar/20120706
-rw-r--r-- 3 hadoop supergroup 0 2013-11-06 15:54 /user/root/input/foo/bar/20120706/_SUCCESS
drwxr-xr-x - hadoop supergroup 0 2013-11-06 15:54 /user/root/input/foo/bar/20120706/_logs
drwxr-xr-x - hadoop supergroup 0 2013-11-06 15:54 /user/root/input/foo/bar/20120706/_logs/history

在 Java 代码中,我使用 PatternMatcher 类来获取稍后要处理的子字符串。代码如 list 所示:

String filename = "D:\\temp\\files_in_hadoop_temp.txt";
Pattern thePattern
= Pattern.compile("[a-z\\-]+\\s+(\\-|[0-9]) (root|hadoop)\\s+supergroup\\s+([0-9]+) ([0-9\\-]+) ([0-9:]+) (\\D+)\\/?.*");

try
{
Files.lines(Paths.get(filename))
.map(line -> thePattern.matcher(line))
.collect(Collectors.toList())
.forEach(theMather -> {
if (theMather.find())
{
System.out.println(theMather.group(3) + "-" + theMather.group(4) + "-" + theMather.group(6));
}
});
} catch (IOException e)
{
e.printStackTrace();
}

结果如下:

0-2015-04-05-/user/root
0-2014-11-05-/user/root/input
0-2014-11-05-/user/root/input/foo
0-2015-04-28-/user/root/input/foo/bar
0-2013-11-06-/user/root/input/foo/bar/
0-2013-11-06-/user/root/input/foo/bar/
0-2013-11-06-/user/root/input/foo/bar/
0-2013-11-06-/user/root/input/foo/bar/

但我的预期结果是没有尾部“/”作为前三行。我尝试了很多模式来去除尾部“/”但失败了。

请您提供一些关于去除尾部“/”的模式的建议。

非常感谢。

最佳答案

使用字符集确保最后一个字符不是斜杠。因此,而不是

(\\D+)\\/?.*"

尝试

(\\D*[^\\d/]).*

括号中的部分匹配最长的非数字子串,并附加限制,最后一个字符不能是斜杠。

注意:已测试。

关于java - 如何使用Java获取带有模式的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29934043/

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