java - Parsing comma-separated values containing quoted commas and newlines -
i have string special characters. aim retrieve string[] of each line (, separated) have special character “ can have /n , ,
for example main string alpha,beta,gama,"23-5-2013,tom",toto,"julie, kamel titi",god," timmy, tomy,tony, tini".
you can see there /n in "".
can me parse this.
thanks
__ more explanation
with main sting need separate these
here alpha beta gama 23-5-2013,tom toto julie,kamel,titi god timmy, tomy,tony,tini
problem : julie,kamel,titi there line break /n or
in between kamel , titi similar problem timmy, tomy,tony,tini there line break /n or
in between tony , tini.
new text in file (compulsory line line reading)
alpha,beta charli,delta,delta echo ,frank george,henry 1234-5,"ida, john ", 25/11/1964, 15/12/1964,"40,000,000.00",0.0975,2,"king, lincoln ",mary / new york,123456 12543-01,"ocean, peter
output want remove "
alpha beta charli delta delta echo frank george henry 1234-5 ida john " 25/11/1964 15/12/1964 40,000,000.00 0.0975 2 king lincoln " mary / new york 123456 12543-01 ocean peter
try this:
string source = "alpha,beta,gama,\"23-5-2013,tom\",toto,\"julie, kamel\n" + "titi\",god,\" timmy, tomy,tony,\n" + "tini\"."; pattern p = pattern.compile("(([^\"][^,]*)|\"([^\"]*)\"),?"); matcher m = p.matcher(source); while(m.find()) { if(m.group(2) != null) system.out.println( m.group(2).replace("\n", "") ); else if(m.group(3) != null) system.out.println( m.group(3).replace("\n", "") ); }
if matches string without quotes, result returned in group 2. strings quotes returned in group 3. hence needed distinction in while-block. might find prettier way.
output:
alpha
beta
gama
23-5-2013,tom
toto
julie, kameltiti
god
timmy, tomy,tony,tini
.
Comments
Post a Comment