fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11.  
  12. public static List<String> getList(String value)
  13. {
  14. String regex = "(?:(?:^|,|\\r?\\n)\\s*)(?:(?:(\"[^\"\\\\]*(?:\\\\[\\S\\s][^\"\\\\]*)*\"|“[^“”\\\\]*(?:\\\\[\\S\\s][^“”\\\\]*)*”))(?:\\s*(?:(?=,|\\r?\\n)|$))|([^,]*)(?:\\s*(?:(?=,)|$)))";
  15. List<String> allMatches = new ArrayList<String>();
  16. if ( value.length() > 0 )
  17. {
  18. Matcher m = Pattern.compile( regex ).matcher( value );
  19. while ( m.find() ) {
  20. String str = m.group(2);
  21. if ( str == null ) {
  22. str = m.group(1);
  23. str = str.replaceAll( "^[\"“”]|[\"“”]$", "" );
  24. }
  25. allMatches.add(str.trim());
  26. }
  27. }
  28. return allMatches;
  29. }
  30.  
  31.  
  32. public static void main (String[] args) throws java.lang.Exception
  33. {
  34. List<String> result = getList("400,test,\"QT_don't split, this_QT\",15");
  35. System.out.println( result );
  36.  
  37. result = getList("500,test,“LQT_don't split, this_RQT”,15");
  38. System.out.println( result );
  39.  
  40. result = getList("600,test,\"QT_don't split, this_QT\",15");
  41. System.out.println( result );
  42.  
  43. }
  44. }
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
[400, test, QT_don't split, this_QT, 15]
[500, test, LQT_don't split, this_RQT, 15]
[600, test, QT_don't split, this_QT, 15]