-2

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= "清理" 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="从服务器 ftp.myftp 下载大小为 550k 的文件 resume.doc。com 无法完成,因为文件感染了病毒 codevirus"

现在我希望将上面的字符串拆分并根据键值对给我输出,如下所示:

array[0]=date=2011-07-08

array[1]=time=10:55:06

array[2]=timezone="IST"

array[3]=device_name='CR1000i"

.......

.......

所以请帮助我..谢谢

4

3 回答 3

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().

于 2012-06-27T13:23:07.000 回答
1

just for an inspiration consider this regex: \w*=(["][^"]*["]|[^ ]*) it matches any number of word (\w) characters, equals sign, and anything in the quotes or anything to the first space.. It matches your example, but surely there will be something that this regex will be too simple for :)

于 2012-06-27T12:58:19.920 回答
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);
}
于 2012-06-28T06:10:17.140 回答