java正则解析读取csv文件

private static final String SPECIAL_CHAR_A = "[^\",//n  ]";

private static final String SPECIAL_CHAR_B = "[^\",//n]";
/**
	 * 构建解析csv文件正则表达式
	 * @return
	 */
    private String getRegExp() {
        StringBuffer strRegExps = new StringBuffer();
        strRegExps.append("\"((");
        strRegExps.append(SPECIAL_CHAR_A);
        strRegExps.append("*[,//n  ])*(");
        strRegExps.append(SPECIAL_CHAR_A);
        strRegExps.append("*\"{2})*)*");
        strRegExps.append(SPECIAL_CHAR_A);
        strRegExps.append("*\"[  ]*,[  ]*");
        strRegExps.append("|");
        strRegExps.append(SPECIAL_CHAR_B);
        strRegExps.append("*[  ]*,[  ]*");
        strRegExps.append("|\"((");
        strRegExps.append(SPECIAL_CHAR_A);
        strRegExps.append("*[,//n  ])*(");
        strRegExps.append(SPECIAL_CHAR_A);
        strRegExps.append("*\"{2})*)*");
        strRegExps.append(SPECIAL_CHAR_A);
        strRegExps.append("*\"[  ]*");
        strRegExps.append("|");
        strRegExps.append(SPECIAL_CHAR_B);
        strRegExps.append("*[  ]*");

        return strRegExps.toString();
    }


ins为InputStream读取的文件流

BufferedReader reader = new BufferedReader(new InputStreamReader(ins, "GBK"));
					String line;
					List<String> cells = new ArrayList<String>();// 每行记录一个list
					while ((line = reader.readLine()) != null) {
						Pattern pCells = Pattern.compile(getRegExp());
						Matcher mCells = pCells.matcher(line);

						while (mCells.find()) {
							str = mCells.group();
							str = str.trim();
							if (str.endsWith(",")) {
								str = str.substring(0, str.length() - 1);
								str = str.trim();
							}

							if (str.startsWith("\"") && str.endsWith("\"")) {
								str = str.substring(1, str.length() - 1);
								if (str.indexOf("\"\"") >= 0) {
									str = str.replaceAll("\"\"", "\"");
								}
							}
							cells.add(str);
						}


你可能感兴趣的:(java正则解析csv)