fork download
  1. import java.util.*;
  2. import java.util.regex.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. String line = "A,,B";
  11. ArrayList<String> tokens = new ArrayList<String>();
  12. String regex = "\"([^\"]*)\"|[^,]+|(?<=,)(?=,)";
  13. Matcher m = Pattern.compile(regex).matcher(line);
  14. while (m.find()) {
  15. if (m.group(1) != null) {
  16. tokens.add(m.group(1));
  17. }
  18. else {
  19. tokens.add(m.group(0));
  20. }
  21. }
  22. System.out.println(tokens);
  23. }
  24. }
  25.  
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
[A, , B]