2014-09-28 56 views
-1

我想提取从HTML发布请求中获得的逗号分隔值。从HTTP发布的字符串中提取逗号分隔值

实施例:

****"file"; filename="TEST.csv" 
Content-Type: application/vnd.ms-excel 
RECEIVERID,RECEIVERSITE,SENDERID,EMAIL_TO 
1000003884,PEPSICO LTD.,1005,[email protected] 
1000003884,PEPSICO LTD.,1010,[email protected] 
------WebKitFormBoundary5ADzaHQgUvmKoZPI 
Content-Disposition: form-data; name="submit" 
Submit 
------WebKitFormBoundary5ADzaHQgUvmKoZPI--**** 

欲仅提取数据通过使用正则表达式从RECEIVERID,RECEIVERSITE开始....到,ABC @ company.com

您能否帮我解决这个问题?

在此先感谢! Tanveer

回答

1

使用String.split(delimiter)它将返回一个包含由指定分隔符分隔的所有String的数组。

更多信息,请参见the doc about String

注意,第一个和最后分裂字符串将包含文本,你不想让你可以随心所欲使用分割方法之前,去除未格式化为VALUE,VALUE,VALUE部分。


如果你真的想使用正则表达式,你可以(再次删除多余的数据后),这样来做:

String regex = "[\\n|^|,]*(.*?)[,|$|\\n]"; 
Pattern pattern = Pattern.compile(regex); 
Matcher matcher = pattern.matcher("RECEIVERID,RECEIVERSITE,SENDERID,EMAIL_TO\n" + 
            "1000003884,PEPSICO LTD.,1005,[email protected]\n" + 
            "1000003884,PEPSICO LTD.,1010,[email protected]"); 

while (matcher.find()) 
    System.out.println(matcher.group(1)); 
+0

什么的'[\\ N |^|,]的意思'正则表达式? – 2014-09-28 15:10:21

0

如果您使用的锚在您的正则^$那么你需要启用(?m)Multiline modifier

String text = "****\"file\"; filename=\"TEST.csv\"\n" + 
     "Content-Type: application/vnd.ms-excel\n" + 
     "RECEIVERID,RECEIVERSITE,SENDERID,EMAIL_TO\n" + 
     "1000003884,PEPSICO LTD.,1005,[email protected]\n" + 
     "1000003884,PEPSICO LTD.,1010,[email protected]\n" + 
     "------WebKitFormBoundary5ADzaHQgUvmKoZPI\n" + 
     "Content-Disposition: form-data; name=\"submit\"\n" + 
     "Submit\n" + 
     "------WebKitFormBoundary5ADzaHQgUvmKoZPI--****"; 
final Pattern p = Pattern.compile("(?m)^[^,\\n]*(?:,[^,]*?)+?$"); 
final Matcher m = p.matcher(text); 
while(m.find()){   
    System.out.println(m.group(0)); 
} 

输出:

RECEIVERID,RECEIVERSITE,SENDERID,EMAIL_TO 
1000003884,PEPSICO LTD.,1005,[email protected] 
1000003884,PEPSICO LTD.,1010,[email protected] 

DEMO

相关问题