用于在java中使用正则表达式拆分字符串

用于在java中使用正则表达式拆分字符串,java,regex,pattern-matching,Java,Regex,Pattern Matching,日期=2011-07-08时间=10:55:06时区=IST“device”device\u name=“CR1000i”device\u id=C010600504-TYGJD3 deployment\u mode=“Route”log\u id=0310062090001 log\u type=“Anti Virus”log\u component=“FTP”log\u subtype=“Clean”status=“Denied”priority=Critical fw\u rule\u i

日期=2011-07-08时间=10:55:06时区=IST“device”device\u name=“CR1000i”device\u id=C010600504-TYGJD3 deployment\u mode=“Route”log\u id=0310062090001 log\u type=“Anti Virus”log\u component=“FTP”log\u subtype=“Clean”status=“Denied”priority=Critical fw\u rule\u id=“user\u name=“hemant”Virus=“codevirus”FTP\u URL=“FTP.myftp.com”FTP\u direction=“下载”文件名=“hemantresume.doc”file_size=“550k”file_path=“deepti/Shortcut to virus.lnk”ftpcomand=“RETR”src_ip=10.103.6.100 dst_ip=10.103.6.66协议=“TCP”src_port=2458 dst_port=21 dstddomain=“myftp.cpm”sent_bytes=162 recv_bytes=45消息=”无法完成从服务器FTP.myftp.com FTP下载大小为550k的resume.doc文件,因为该文件感染了病毒代码“virus”

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

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

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

array[2]=timezone="IST"

array[3]=device_name='CR1000i"

.......

.......

请帮助我…谢谢

< P>只是为了灵感,考虑这个正则表达式:<代码> \W*=([])[^ ] *[] ] [^ ^ ] < <代码> 它匹配任意数量的单词(\w)字符、等号、引号中的任何内容或第一个空格中的任何内容。。它与您的示例相匹配,但肯定会有一些事情是这个正则表达式太简单了:)

您可以使用类似@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());
}
String text=“dstdomain=\”myftp.cpm\”发送的\u字节=162 recv\u字节=45消息=\”无法完成从服务器FTP.myftp.com FTP下载大小为550k的resume.doc文件,因为该文件感染了病毒代码病毒\”;
String patternText=“\\w+=([^\“]+\\”[^\“]*\”);
Matcher Matcher=Pattern.compile(patternText).Matcher(text);
列表匹配项=新的ArrayList();
while(matcher.find()){
matches.add(matcher.group());
}

如果出于某种原因确实需要将结果作为
数组
,您可以使用
matches.toArray()

查找以下代码。请注意,代码是用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);
}
StringBuilder testt=new StringBuilder(“日期=2011-07-08时间=10:55:06时区=\'IST\'设备\u名称=\'CR1000i\'设备\u id=C010600504-TYGJD3部署\u模式=\'Route\'日志\u id=031006209001日志\u类型=\'Anti Virus\'日志\u组件=\'FTP\'日志\u子类型='Clean\'状态='Denied\'优先级=关键fw\'u规则\u id='codevirus\'FTP\'uURL=\'FTP.myftp.com\'FTP\'u direction=\'download\'filename=\'hemantresume.doc\'file\'size=\'550k\'file\'path=\'depti/Shortcut to virus.lnk\'ftpcommand=\'RETR\'src\'u ip=10.103.6.100 dst\'ip=10.103.6.66协议='TCP\'src\'u端口=2458 dst\'port=21 dst域='myftp.cpm\'sent=162字节=45接收报文无法完成从服务器FTP.myftp.com下载大小为550k的resume.doc文件,因为该文件感染了病毒代码virus\“”);
Pattern varPattern=Pattern.compile(“[A-Z_]+=”,Pattern.CASE不区分大小写);
Matcher-varMatcher=varPattern.Matcher(testt);
列表=新的ArrayList();
int startIndex=0,endIndex=0;
布尔值=false;
while(varMatcher.find()){
endIndex=varMatcher.start();
添加(testt.substring(startIndex,endIndex));
startIndex=varMatcher.start();
发现=真;
}
如果(找到){
添加(testt.substring(startIndex));
}
用于(字符串s:列表){
系统输出打印项次;
}

您尝试过什么?你至少读过String类的javadoc了吗?@asha-两个令牌中有空格吗?i、 e.btn日期=2011-07-08,时间=10:55:06?引号中可以有空格。这是主要的困难。例如,请参阅键
消息
。然后,引号中甚至可能有转义引号,同样的老故事开始了。如果没有被quotes@JBNizet..我试过这个..但它没有给出数字字符串。。字符串数组[]=str.split(“(\\w)(\”)@Hlopik…当我使用上面的regexe进行拆分时,它什么也没有给我..全部为空它不是用于split方法,而是用于matcher,如beauty@Keppil的答案所示:)@Saurabh..谢谢..它像一个符咒一样工作。。。