fork download
  1. import java.util.regex.*;
  2. import java.util.*;
  3. import java.util.stream.*; //Collectors
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. String regex = "cgcg";
  10. Pattern pattern = Pattern.compile("(?=(" + regex + "))");
  11. Matcher matcher = pattern.matcher("cgcgcgcgcgcg");
  12. while (matcher.find())
  13. System.out.println(matcher.start(1));
  14.  
  15. List<Integer> results = pattern.matcher("cgcgcgcgcgcg")
  16. .results()
  17. .map(r -> r.start(1))
  18. .collect(Collectors.toList());
  19. System.out.println(results);
  20. }
  21. }
Success #stdin #stdout 0.11s 50812KB
stdin
Standard input is empty
stdout
0
2
4
6
8
[0, 2, 4, 6, 8]