给定字符串:
<180>May 19 17:54:27 ise121 CISE_Internal_Operations_Diagnostics 0000046406 2 0 2015-05-19 17:54:27.930 +05:30 0000290168 34126 WARN System-Management: Remote syslog target is unavailable
我的RegEx:
\\<(\\d+)\\>(\\w+[\\d\\s\\:]+\\S+\\s\\S+\\s(\\d+)\\s(\\d+)\\s(\\d+)\\s([\\d\\:\\-\\.\\+\\s]{0,30})\\s+\\d+)\\s+(\\d+)\\s+(\\S+)\\s+(\\S+\\:.*?\\,)(.*)
但这不起作用所以请帮忙
代码:
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchMoreLogsFromSingleRegEx {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = "<180>May 19 17:54:27 ise121 CISE_Internal_Operations_Diagnostics 0000046406 2 0 2015-05-19 17:54:27.930 +05:30 0000290168 34126 WARN System-Management: Remote syslog target is unavailable";
Pattern pattern = Pattern
.compile("\\<(\\d+)\\>(\\w+[\\d\\s\\:]+\\S+\\s\\S+\\s(\\d+)\\s(\\d+)\\s(\\d+)\\s([\\d\\:\\-\\.\\+\\s]{0,30})\\s+\\d+)\\s+(\\d+)\\s+(\\S+)\\s+(\\S+\\:.*?\\,)(.*)");
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
System.out.println("true");
StringBuilder sb = new StringBuilder();
int cnt = matcher.groupCount();
System.out.println(cnt);
for (int i = 1; i <= cnt; ++i) {
System.out.println(matcher.group(i) + "\t" + i);
}
} else {
System.out.println("false");
}
}
}
答案 0 :(得分:2)
它无效,因为在子串System Management:
之后不存在逗号。所以删除最后一个逗号。
\\<(\\d+)\\>(\\w+[\\d\\s\\:]+\\S+\\s\\S+\\s(\\d+)\\s(\\d+)\\s(\\d+)\\s([\\d\\:\\-\\.\\+\\s]{0,30})\\s+\\d+)\\s+(\\d+)\\s+(\\S+)\\s+(\\S+\\:.*?)(.*)
我想你想要这样的东西。在这里,我将逗号部分设为可选部分。
\<(\d+)\>(\w+[\d\s\:]+\S+\s\S+\s(\d+)\s(\d+)\s(\d+)\s([\d\:\-\.\+\s]{0,30})\s+\d+)\s+(\d+)\s+(\S+)\s+(\S+\:(?:.*?,)?)(.*)
在java中再次使用反斜杠。