date = 2011-07-08 time = 10:55:06 timezone =“IST”device_name =“CR1000i”device_id = C010600504-TYGJD3 deployment_mode =“Route”log_id = 031006209001 log_type =“Anti Virus”log_component =“FTP “log_subtype =”Clean“status =”Denied“priority = Critical fw_rule_id =”“user_name =”hemant“virus =”codevirus“FTP_URL =”ftp.myftp.com“FTP_direction =”download“filename =”hemantresume.doc“file_size =“550k”file_path =“deepti / virus.lnk的快捷方式”ftpcommand =“RETR”src_ip = 10.103.6.100 dst_ip = 10.103.6.66 protocol =“TCP”src_port = 2458 dst_port = 21 dstdomain =“myftp.cpm”sent_bytes = 162 recv_bytes = 45 message =“文件resume.doc从服务器ftp.myftp.com下载大小550k的FTP下载无法完成,因为文件感染了病毒代码病毒”
现在我希望上面的字符串被拆分,并根据键值对给我输出如下:
array[0]=date=2011-07-08
array[1]=time=10:55:06
array[2]=timezone="IST"
array[3]=device_name='CR1000i"
.......
.......
所以请帮助我......谢谢
答案 0 :(得分:2)
您可以像@Hlopik建议的那样使用正则表达式(几乎),然后循环查找所有匹配项:
String text = "dstdomain=\"myftp.cpm\" sent_bytes=162 recv_bytes=45 message=\"An FTP download of File resume.doc of size 550k from server ftp.myftp.com could not be completed as file was infected with virus codevirus\"";
String patternText = "\\w+=([^ \"]+|\"[^\"]*\")";
Matcher matcher = Pattern.compile(patternText).matcher(text);
List<String> matches = new ArrayList<String>();
while (matcher.find()) {
matches.add(matcher.group());
}
如果您因某种原因确实需要Array
的结果,可以使用matches.toArray()
。
答案 1 :(得分:1)
只是为了灵感考虑这个正则表达式:\w*=(["][^"]*["]|[^ ]*)
它匹配任意数量的单词(\ w)字符,等号,以及引号中的任何内容或第一个空格中的任何内容。它与您的示例相匹配,但肯定会有一些东西,这个正则表达式太简单了:)< / p>
答案 2 :(得分:1)
请找到以下代码。请注意,代码在JAVA中。
StringBuilder testt = new StringBuilder("date=2011-07-08 time=10:55:06 timezone=\"IST\" device_name=\"CR1000i\" device_id=C010600504-TYGJD3 deployment_mode=\"Route\" log_id=031006209001 log_type=\"Anti Virus\" log_component=\"FTP\" log_subtype=\"Clean\" status=\"Denied\" priority=Critical fw_rule_id=\"\" user_name=\"hemant\" virus=\"codevirus\" FTP_URL=\"ftp.myftp.com\" FTP_direction=\"download\" filename=\"hemantresume.doc\" file_size=\"550k\" file_path=\"deepti/Shortcut to virus.lnk\" ftpcommand=\"RETR\" src_ip=10.103.6.100 dst_ip=10.103.6.66 protocol=\"TCP\" src_port=2458 dst_port=21 dstdomain=\"myftp.cpm\" sent_bytes=162 recv_bytes=45 message=\"An FTP download of File resume.doc of size 550k from server ftp.myftp.com could not be completed as file was infected with virus codevirus\"");
Pattern varPattern = Pattern.compile("[A-Z_]+=", Pattern.CASE_INSENSITIVE);
Matcher varMatcher = varPattern.matcher(testt);
List<String> list = new ArrayList<String>();
int startIndex = 0, endIndex=0;
boolean found=false;
while (varMatcher.find()) {
endIndex = varMatcher.start();
list.add(testt.substring(startIndex, endIndex));
startIndex= varMatcher.start();
found=true;
}
if(found){
list.add(testt.substring(startIndex));
}
for(String s:list){
System.out.println(s);
}