java - how to filter out some substrings with regex effectively? -
i want filter out srcport , dstport input string. here code tried:
string input = "2014<>10.100.2.3<><189>date=2014-01-16,time=11:26:14,devname=b3909601569,devid=b3909601569,logid=000013,type=traffic,srcip=192.168.192.123,srcport=2072,srcintf=port2,dstip=10.180.1.105,dstport=3206,dstintf=port1,sessionid=121543,status=close,policyid=196,service=mysql,proto=6,duration=10,sentbyte=3910,rcvdbyte=175085,sentpkt=74,rcvdpkt=132"; pattern p = pattern.compile("(srcport=)(\\d+).[\\s]?(dstport=)(\\d+)"); matcher m = p.matcher(input); stringbuffer result=new stringbuffer(); while (m.find()) { system.out.println("srcport: " + m.group(2) + " & "); system.out.println("dstport: " + m.group(4)); } system.out.println(result);
but not showing output. there mistake in regex
pattern p = pattern.compile("(srcport=)(\\d+).[\\s]?(dstport=)(\\d+)");
or println lines
system.out.println("srcport: " + m.group(2) + " & "); system.out.println("dstport: " + m.group(4));"
any suggestions highly appreciated.
see following changes both regex , captured groups:
string input = "2014<>10.100.2.3<><189>date=2014-01-16,time=11:26:14,devname=b3909601569,devid=b3909601569,logid=000013,type=traffic,srcip=192.168.192.123,srcport=2072,srcintf=port2,dstip=10.180.1.105,dstport=3206,dstintf=port1,sessionid=121543,status=close,policyid=196,service=mysql,proto=6,duration=10,sentbyte=3910,rcvdbyte=175085,sentpkt=74,rcvdpkt=132"; pattern p = pattern.compile("srcport=(\\d+).*?dstport=(\\d+)"); // update regex matcher m = p.matcher(input); stringbuffer result=new stringbuffer(); while (m.find()) { system.out.println("srcport: " + m.group(1)); //print groups 1 + 2 system.out.println("dstport: " + m.group(2)); } system.out.println(result);
Comments
Post a Comment