fork download
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. class Ideone
  6. {
  7.  
  8. private static List<Integer> indices = new ArrayList<Integer>();
  9. private static List<String> result = new ArrayList<String>();
  10.  
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13.  
  14. String str = "a:b?:c??:d???:e????:f";
  15.  
  16. Pattern pattern = Pattern.compile("(?<!\\?)(?:\\?{2})*:");
  17. Matcher matcher = pattern.matcher(str);
  18.  
  19. while(matcher.find()) {
  20. result.add(str.substring(getLastIndex(), matcher.end() - 1));
  21. indices.add(matcher.end());
  22. }
  23. result.add(str.substring(getLastIndex()));
  24. System.out.print(result);
  25. }
  26.  
  27. private static int getLastIndex() {
  28. if(indices.isEmpty()) {
  29. return 0;
  30. } else {
  31. return indices.get(indices.size() - 1);
  32. }
  33. }
  34. }
Success #stdin #stdout 0.04s 4575232KB
stdin
Standard input is empty
stdout
[a, b?:c??, d???:e????, f]