regex - Parsing using Pattern in Java -
i want parse lines of file using parsingmethod
test.csv
frank george,henry,mary / new york,123456 ,beta charli,"delta,delta echo ", 25/11/1964, 15/12/1964,"40,000,000.00",0.0975,2,"king, lincoln ",alpha this way read line
public static void main(string[] args) throws exception { file file = new file("c:\\users\\test.csv"); bufferedreader reader = new bufferedreader(new filereader(file)); string line2; while ((line2= reader.readline()) !=null) { string[] tab = parsingmethod(line2, ","); (string : tab) { system.out.println( ); } } } public static string[] parsingmethod(string line,string parser) { list<string> liste = new linkedlist<string>(); string patternstring ="(([^\"][^"+parser+ "]*)|\"([^\"]*)\")" +parser+"?"; pattern pattern = pattern.compile(patternstring); matcher matcher =pattern.matcher(line); while (matcher.find()) { if(matcher.group(2) != null){ liste.add(matcher.group(2).replace("\n","").trim()); }else if(matcher.group(3) != null){ liste.add(matcher.group(3).replace("\n","").trim()); } } string[] result = new string[liste.size()]; return liste.toarray(result); } } output :
frank george henry mary / new york 123456 beta charli delta delta echo " 25/11/1964 15/12/1964 40,000,000.00 0.0975 2 king lincoln " alpha delta delta echo i want remove " , can 1 me improve pattern.
expected output
frank george henry mary / new york 123456 beta charli delta delta echo 25/11/1964 15/12/1964 40,000,000.00 0.0975 2 king lincoln alpha delta delta echo output line 3
25/11/1964 15/12/1964 40 000 000.00 0.0975 2 king lincoln
your code didn't compile caused of " not being escaped.
but should trick:
string patternstring = "(?:^.,|)([^\"]*?|\".*?\")(?:,|$)"; pattern pattern = pattern.compile(patternstring, pattern.multiline); (?:^.,|) non capturing group matches single character @ start of line
([^\"]*?|\".*?\") capturing group either matches " or in between " "
(?:,|$) non capturing group matches end of line or comma.
note: ^ , $ work stated when pattern compiled pattern.multiline flag
Comments
Post a Comment