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.*;
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. char char1 = '|', char2 = ')';
  13. String str = "a,b,c,d," + char1 + "e,f,g" + char2 + ",h";
  14. String ch1_quoted = Pattern.quote(Character.toString(char1));
  15. String ch2_quoted = Pattern.quote(Character.toString(char2));
  16. List<String> s2 = new ArrayList<>();
  17. Pattern pattern = Pattern.compile(ch1_quoted + "(.*?)"
  18. + ch2_quoted + "|[^," + ch1_quoted + ch2_quoted + "]+", Pattern.DOTALL);
  19. Matcher matcher = pattern.matcher(str);
  20. while (matcher.find()){
  21. if (matcher.group(1) != null) {
  22. s2.add(matcher.group(1));
  23. System.out.println(matcher.group(1));
  24. } else {
  25. s2.add(matcher.group(0));
  26. System.out.println(matcher.group(0));
  27. }
  28.  
  29. }
  30.  
  31. }
  32. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
a
b
c
d
e,f,g
h