我试图使用正则表达式解析一些日志消息以查找用户ID,源MAC,源IP,源端口等。我能够使用3种不同的正则表达式解析必要的数据。但是我无法将它们合并为一个。
我发现了这个问题,他建议在answer表达式之间使用|
,但是使用|
仅给出第一个表达式的值,{{1之后的表达式的值}}显示|
我的代码:
null
输出:
public class LogParser {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String mydata = "firewall,info Rural: forward: in:<pppoe-Abrar> out:wan_2, src-mac c0:4a:00:b4:7a:71, proto TCP (ACK), 192.168.4.254:51409->216.58.221.202:443, NAT (192.168.4.254:51409->192.168.43.211:51409)->216.58.221.202:443, len 52";
pattern = Pattern.compile("<(.*?)>|src-mac(.*?), proto (.*?),|NAT \\((.*?):(.*?)->(.*?):(.*?)\\)->(.*?):(.*?),");
matcher = pattern.matcher(mydata);
if (matcher.find())
{
System.out.println(matcher.groupCount());
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
System.out.println(matcher.group(4));
System.out.println(matcher.group(5));
System.out.println(matcher.group(6));
System.out.println(matcher.group(7));
System.out.println(matcher.group(8));
System.out.println(matcher.group(9));
}
}
}
答案 0 :(得分:1)
您需要更改模式以使其正常工作。
尝试一下:
package testPkg;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LogParser {
public static void main(String[] args) {
String mydata = "firewall,info Rural: forward: in:<pppoe-Abrar> out:wan_2, src-mac c0:4a:00:b4:7a:71, proto TCP (ACK), 192.168.4.254:51409->216.58.221.202:443, NAT (192.168.4.254:51409->192.168.43.211:51409)->216.58.221.202:443, len 52";
Pattern pattern = Pattern.compile("firewall,info Rural: forward: in:<(.*?)> out:wan_2, src-mac (.*?), proto (.*?), (.*?)->(.*?), NAT \\((.*?)->(.*?)\\)->(.*?), len (.*?)$");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
System.out.println("Total number of groups macthed: " + matcher.groupCount());
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println(matcher.group(i));
}
}
}
}
结果:
9
pppoe-Abrar
c0:4a:00:b4:7a:71
TCP (ACK)
192.168.4.254:51409
216.58.221.202:443
192.168.4.254:51409
192.168.43.211:51409
216.58.221.202:443
52
答案 1 :(得分:0)
怎么样呢?
groovy:000> myData = "firewall,info Rural: forward: in:<pppoe-Abrar> out:wan_2, src-mac c0:4a:00:b4:7a:71, proto TCP (ACK), 192.168.4.254:51409->216.58.221.202:443, NAT (192.168.4.254:51409->192.168.43.211:51409)->216.58.221.202:443, len 52"
groovy:000> regexp = "(<([\\w-]+)>).*?(src-mac ([\\w:]+), proto ([^,]+),).*?(NAT \\(([\\d:.]+)->([\\d:.]+)\\)->([\\d.:]+))"
groovy:000> pattern = java.util.regex.Pattern.compile(regexp);
groovy:000> matcher = pattern.matcher(myData)
groovy:000> matcher.find()
===> true
groovy:000> for (int i=1; i <= matcher.groupCount(); i++) { print matcher.group(i) + "\n"; }
<pppoe-Abrar>
pppoe-Abrar
src-mac c0:4a:00:b4:7a:71, proto TCP (ACK),
c0:4a:00:b4:7a:71
TCP (ACK)
NAT (192.168.4.254:51409->192.168.43.211:51409)->216.58.221.202:443
192.168.4.254:51409
192.168.43.211:51409
216.58.221.202:443
===> null