Regex to pick characters outside of pair of quotes

前端 未结 6 1458
广开言路
广开言路 2020-11-22 00:29

I would like to find a regex that will pick out all commas that fall outside quote sets.

For example:

\'foo\' => \'bar\',
\'foofoo\' => \'bar,         


        
6条回答
  •  逝去的感伤
    2020-11-22 01:22

    MarkusQ's answer worked great for me for about a year, until it didn't. I just got a stack overflow error on a line with about 120 commas and 3682 characters total. In Java, like this:

            String[] cells = line.split("[\t,](?=(?:[^\"]|\"[^\"]*\")*$)", -1);
    

    Here's my extremely inelegant replacement that doesn't stack overflow:

    private String[] extractCellsFromLine(String line) {
        List cellList = new ArrayList();
        while (true) {
            String[] firstCellAndRest;
            if (line.startsWith("\"")) {
                firstCellAndRest = line.split("([\t,])(?=(?:[^\"]|\"[^\"]*\")*$)", 2);
            }
            else {
                firstCellAndRest = line.split("[\t,]", 2);                
            }
            cellList.add(firstCellAndRest[0]);
            if (firstCellAndRest.length == 1) {
                break;
            }
            line = firstCellAndRest[1];
        }
        return cellList.toArray(new String[cellList.size()]);
    }
    

提交回复
热议问题