Java: splitting a comma-separated string but ignoring commas in quotes

前端 未结 11 1534
广开言路
广开言路 2020-11-21 05:16

I have a string vaguely like this:

foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"

that I want to split by commas -- but I need to igno

相关标签:
11条回答
  • 2020-11-21 06:08

    I was impatient and chose not to wait for answers... for reference it doesn't look that hard to do something like this (which works for my application, I don't need to worry about escaped quotes, as the stuff in quotes is limited to a few constrained forms):

    final static private Pattern splitSearchPattern = Pattern.compile("[\",]"); 
    private List<String> splitByCommasNotInQuotes(String s) {
        if (s == null)
            return Collections.emptyList();
    
        List<String> list = new ArrayList<String>();
        Matcher m = splitSearchPattern.matcher(s);
        int pos = 0;
        boolean quoteMode = false;
        while (m.find())
        {
            String sep = m.group();
            if ("\"".equals(sep))
            {
                quoteMode = !quoteMode;
            }
            else if (!quoteMode && ",".equals(sep))
            {
                int toPos = m.start(); 
                list.add(s.substring(pos, toPos));
                pos = m.end();
            }
        }
        if (pos < s.length())
            list.add(s.substring(pos));
        return list;
    }
    

    (exercise for the reader: extend to handling escaped quotes by looking for backslashes also.)

    0 讨论(0)
  • 2020-11-21 06:11

    I would do something like this:

    boolean foundQuote = false;
    
    if(charAtIndex(currentStringIndex) == '"')
    {
       foundQuote = true;
    }
    
    if(foundQuote == true)
    {
       //do nothing
    }
    
    else 
    
    {
      string[] split = currentString.split(',');  
    }
    
    0 讨论(0)
  • 2020-11-21 06:12

    Try a lookaround like (?!\"),(?!\"). This should match , that are not surrounded by ".

    0 讨论(0)
  • 2020-11-21 06:12

    Rather than use lookahead and other crazy regex, just pull out the quotes first. That is, for every quote grouping, replace that grouping with __IDENTIFIER_1 or some other indicator, and map that grouping to a map of string,string.

    After you split on comma, replace all mapped identifiers with the original string values.

    0 讨论(0)
  • 2020-11-21 06:15

    http://sourceforge.net/projects/javacsv/

    https://github.com/pupi1985/JavaCSV-Reloaded (fork of the previous library that will allow the generated output to have Windows line terminators \r\n when not running Windows)

    http://opencsv.sourceforge.net/

    CSV API for Java

    Can you recommend a Java library for reading (and possibly writing) CSV files?

    Java lib or app to convert CSV to XML file?

    0 讨论(0)
提交回复
热议问题