fork(1) download
  1. /* Separar texto por comas, excepto entre comillas, con expresiones regulares
  2.   https://es.stackoverflow.com/q/65502/127 */
  3.  
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11.  
  12. final String regex = ",(\"[^\"]*\"|[^,]*)";
  13. final String text = ",10010222,\"The Royal Bank of Scotland, Niederlassung Deutschland\",10105,Berlin";
  14.  
  15. final Pattern pattern = Pattern.compile(regex);
  16. final Matcher matcher = pattern.matcher("," + text);
  17. int n = 0;
  18.  
  19. System.out.println("Texto original: " + text);
  20.  
  21. while (matcher.find()) {
  22. System.out.print ("Elemento " + ++n + ": ");
  23. System.out.println(matcher.group(1));
  24. }
  25. }
  26. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
Texto original: ,10010222,"The Royal Bank of Scotland, Niederlassung Deutschland",10105,Berlin
Elemento 1: 
Elemento 2: 10010222
Elemento 3: "The Royal Bank of Scotland, Niederlassung Deutschland"
Elemento 4: 10105
Elemento 5: Berlin