集册 Java实例教程 用regex解析CSV行

用regex解析CSV行

欢马劈雪     最近更新时间:2020-01-02 10:19:05

457
用正则表达式解析CSV行

/**

 * Copyright ? 2010-2012 Atilika Inc.  All rights reserved.

 *

 * Atilika Inc. licenses this file to you under the Apache License, Version

 * 2.0 (the "License"); you may not use this file except in compliance with

 * the License.  A copy of the License is distributed with this work in the

 * LICENSE.txt file.  You may also obtain a copy of the License from

 * 

 *   http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT

 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the

 * License for the specific language governing permissions and limitations

 * under the License.

 */

//package com.nowjava;

import java.util.ArrayList;// 来自 时 代 J a v a 公 众 号

import java.util.regex.Matcher;

import java.util.regex.Pattern;


public class Main {

    private static final char QUOTE = '"';

    private static final char COMMA = ',';

    private static final Pattern QUOTE_REPLACE_PATTERN = Pattern

            .compile("^\"([^\"]+)\"$");

    private static final String ESCAPED_QUOTE = "\"\"";


    /**

     * Parse CSV line

     * @param line

     * @return Array of values

     */

    public static String[] parse(String line) {

        boolean insideQuote = false;

        ArrayList<String> result = new ArrayList<String>();

        int quoteCount = 0;

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < line.length(); i++) {

            char c = line.charAt(i);//来自 时   代    Java - nowjava.com


            if (c == QUOTE) {

                insideQuote = !insideQuote;

                quoteCount++;

            }


            if (c == COMMA && !insideQuote) {

                String value = sb.toString();

                value = unQuoteUnEscape(value);

                result.add(value);

                sb = new StringBuilder();

                continue;

            }


            sb.append(c);

        }


        result.add(sb.toString());


        // Validate

        if (quoteCount % 2 != 0) {

            return new String[0];

        }


        return result.toArray(
展开阅读全文