How can you parse a string with a text qualifier

How can I make out String str = "abc, \"def,ghi\"";

so i get the output like

String[] strs = {"abc", "\"def,ghi\""}

i.e. an array of length 2.

Should I use regex or is there any method in java api or anyother opensource

who allowed me to do this?

Edited

To give a context to the problem, I read a text file that has a list of entries, one on each line. Each entry has a list of fields separated by a separator (comma or semicolon). Now I have a requirement when I have to support a text classifier, something excellent or open office support. Suppose I have a record

abc, "def, ghi"

In this, my separator and "is my text specifier so that when parsing this line I should get two fields abc and def, ghi not {abc, def, ghi}

Hope this clears my claim.

thank

Shekhar

+3
source share
4 answers

The basic algorithm is not too complicated:

 public static List<String> customSplit(String input) {
   List<String> elements = new ArrayList<String>();       
   StringBuilder elementBuilder = new StringBuilder();

   boolean isQuoted = false;
   for (char c : input.toCharArray()) {
     if (c == '\"') {
        isQuoted = !isQuoted;
        // continue;        // changed according to the OP comment - \" shall not be skipped
     }
     if (c == ',' && !isQuoted) {
        elements.add(elementBuilder.toString().trim());
        elementBuilder = new StringBuilder();
        continue;
     }
     elementBuilder.append(c); 
   }
   elements.add(elementBuilder.toString().trim()); 
   return elements;
}
+5
source

This question seems appropriate: Split a string that ignores quoted sections

Along this line http://opencsv.sourceforge.net/ seems appropriate.

+2
source

-

 String str = "abc, \"def,ghi\"";
            String regex = "([,]) | (^[\"\\w*,\\w*\"])";
            for(String s : str.split(regex)){
                System.out.println(s);
            }
0

Try:

List<String> res = new LinkedList<String>();

String[] chunks = str.split("\\\"");
if (chunks.length % 2 == 0) {
    // Mismatched escaped quotes!
}
for (int i = 0; i < chunks.length; i++) {
    if (i % 2 == 1) {
        res.addAll(Array.asList(chunks[i].split(",")));
    } else {
        res.add(chunks[i]);
    }
}

, .

trim(), .

0

Source: https://habr.com/ru/post/1753125/


All Articles