故事 - 从日志文件中提取信息 - 在本例中是maven构建。基本上我想在首先匹配给定模式后获得与模式匹配的所有行。
换句话说,我希望在[WARNING]
行之后匹配以[WARNING] Javadoc Warnings
开头的所有行。
我试过用这个:
(?<=\[WARNING\] Javadoc Warnings).*(\r|\n|.)(?=(\[WARNING\].*))
但它只返回下一行。
[WARNING] E:\workspaces\1
[WARNING] E:\workspaces\4
输入
[WARNING] E:\workspaces\asd
[WARNING] E:\workspaces\dsa
[INFO]
12 warnings
[WARNING] Javadoc Warnings
[WARNING] E:\workspaces\1
[WARNING] E:\workspaces\2
[WARNING] E:\workspaces\3
[INFO] Building jar: E:\workspaces\asdf
[INFO]
[INFO] >>> maven-source-plugin:3.0.1:j
[INFO]
[INFO] <<< maven-source-plugin:3.0.1:j
[INFO]
12 warnings
[WARNING] Javadoc Warnings
[WARNING] E:\workspaces\4
[WARNING] E:\workspaces\5
[WARNING] E:\workspaces\6
[INFO] Building jar: E:\workspaces\asdf
[INFO]
[INFO] >>> maven-source-plugin:3.0.1:j
[INFO]
[INFO] <<< maven-source-plugin:3.0.1:j
,所需的结果是
[WARNING] E:\workspaces\1
[WARNING] E:\workspaces\2
[WARNING] E:\workspaces\3
[WARNING] E:\workspaces\4
[WARNING] E:\workspaces\5
[WARNING] E:\workspaces\6
答案 0 :(得分:2)
您可以使用以下解决方案:
(?:\G(?!\A)|\[WARNING] Javadoc Warnings)\R(\[WARNING].*)
请参阅regex demo
<强>详情
(?:\G(?!\A)|\[WARNING] Javadoc Warnings)
- [WARNING] Javadoc Warnings
子字符串(\[WARNING] Javadoc Warnings
)或(|
)上一次成功匹配的结束(\G(?!\A)
)\R
- 换行符(\[WARNING].*)
- 第1组:[WARNING]
子字符串及其后的任何0+字符到行尾请参阅online Java demo:
String s = "[WARNING] E:\\workspaces\\asd\r\n[WARNING] E:\\workspaces\\dsa\r\n\r\n[INFO] \r\n12 warnings\r\n[WARNING] Javadoc Warnings\r\n[WARNING] E:\\workspaces\\1\r\n[WARNING] E:\\workspaces\\2\r\n[WARNING] E:\\workspaces\\3\r\n\r\n[INFO] Building jar: E:\\workspaces\\asdf\r\n[INFO] \r\n[INFO] >>> maven-source-plugin:3.0.1:j\r\n\r\n[INFO] \r\n[INFO] <<< maven-source-plugin:3.0.1:j\r\n[INFO] \r\n12 warnings\r\n[WARNING] Javadoc Warnings\r\n[WARNING] E:\\workspaces\\4\r\n[WARNING] E:\\workspaces\\5\r\n[WARNING] E:\\workspaces\\6\r\n[INFO] Building jar: E:\\workspaces\\asdf\r\n[INFO] \r\n[INFO] >>> maven-source-plugin:3.0.1:j\r\n[INFO] \r\n[INFO] <<< maven-source-plugin:3.0.1:j";
Pattern pattern = Pattern.compile("(?:\\G(?!\\A)|\\[WARNING] Javadoc Warnings)\\R(\\[WARNING].*)");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
System.out.println(matcher.group(1));
}