我有一个包含以下记录的文件:
--executor-memory 30G --driver-memory 4G --conf spark.shuffle.spill=false --conf spark.storage.memoryFraction=0.1
在Java代码中,我使用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
和Pattern
类来获取我想要稍后处理的子字符串。代码如清单所示:
Matcher
,结果如下:
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 :(得分:1)
使用字符集确保最后一个字符不是斜线。因此,而不是
(\\D+)\\/?.*"
试
(\\D*[^\\d/]).*
括号中的部分匹配非数字的最长子字符串,并添加了限制,即最后一个字符可能不是斜杠。
注意:经过测试。
答案 1 :(得分:0)
如果最后一个字符是斜杠并使用子字符串获取新字符串,则可以检查一个简单的if语句:
if (theMather.find())
{
String data = theMather.group(3) + "-" + theMather.group(4) + "-" + theMather.group(6);
//String data = theMather.group(3) + "-" + theMather.group(4) + "-" + theMather.group(6);
if(data.charAt(data.length() - 1) == '/')
data = data.substring(0, data.length() - 1);
System.out.println(data);
}