正则表达解析CSV文件

 /*

给定字符:dw,"kk,ll",",yioi",iu,",",r3,"""fte",l,"kk""ll",mm'oo,"n""dw,erw"",e",,
要求结果:dw kk,ll ,yioi iu , r3 "fte l kk"ll mm'oo n"dw,erw",e 

*/

 

 

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Test {
   
public static void main(String[] args) {
        String str
= "dw,/"kk,ll/",/",yioi/",iu,/",/",r3,/"/"/"fte/",l,/"kk/"/"ll/",mm'oo,/"n/"/"dw,erw/"/",e/",, ";
        String regex
= "//G(?:^|,)(?:/"([^/"]*+(?:/"/"[^/"]*+)*+)/"|([^/",]*+))";
        Matcher main
= Pattern.compile(regex).matcher(str);
        Matcher mquote
= Pattern.compile("/"/"").matcher("");
       
while (main.find()) {
            String field;
           
if (main.start(2) >= 0) {
                field
= main.group(2);
            }
else {
                field
= mquote.reset(main.group(1)).replaceAll("/"");
            }
            System.out.println(
"Field [" + field + "]");
        }
        System.out.println(
"dw kk,ll ,yioi iu , r3 /"fte l kk/"ll mm'oo n/"dw,erw/",e");
    }
}

你可能感兴趣的:(csv,string,regex,oo,class)