java正则解析读取csv文件

来源:互联网 发布:网络歌手伤感歌曲大全 编辑:程序博客网 时间:2024/05/17 09:11
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>();// 每行记录一个listwhile ((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);}


0 0