Parsing CSV input with a RegEx in java

后端 未结 4 2141
无人及你
无人及你 2020-11-27 16:30

I know, now I have two problems. But I\'m having fun!

I started with this advice not to try and split, but instead to match on what is an acceptable field, and expa

4条回答
  •  北海茫月
    2020-11-27 17:06

    When I started to understand what I had done wrong, I also started to understand how convoluted the lookarounds were making this. I finally realized that I didn't want all the matched text, I wanted specific groups inside of it. I ended up using something very similar to my original RegEx except that I didn't do a lookahead on the closing comma, which I think should be a little more efficient. Here is my final code.

    package regex.parser;
    
    import java.util.ArrayList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class CSVParser {
    
        /*
         * This Pattern will match on either quoted text or text between commas, including
         * whitespace, and accounting for beginning and end of line.
         */
        private final Pattern csvPattern = Pattern.compile("\"([^\"]*)\"|(?<=,|^)([^,]*)(?:,|$)");  
        private ArrayList allMatches = null;    
        private Matcher matcher = null;
        private String match = null;
        private int size;
    
        public CSVParser() {        
            allMatches = new ArrayList();
            matcher = null;
            match = null;
        }
    
        public String[] parse(String csvLine) {
            matcher = csvPattern.matcher(csvLine);
            allMatches.clear();
            String match;
            while (matcher.find()) {
                match = matcher.group(1);
                if (match!=null) {
                    allMatches.add(match);
                }
                else {
                    allMatches.add(matcher.group(2));
                }
            }
    
            size = allMatches.size();       
            if (size > 0) {
                return allMatches.toArray(new String[size]);
            }
            else {
                return new String[0];
            }           
        }   
    
        public static void main(String[] args) {        
            String lineinput = "the quick,\"brown, fox jumps\",over,\"the\",,\"lazy dog\"";
    
            CSVParser myCSV = new CSVParser();
            System.out.println("Testing CSVParser with: \n " + lineinput);
            for (String s : myCSV.parse(lineinput)) {
                System.out.println(s);
            }
        }
    
    }
    

提交回复
热议问题