fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  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. String regex = "(?:https://(\\S*?)/v(\\S*?)/|\\G(?!^))([^/\\s]+)/?";
  12. String string = "https://123/v1/a/b/";
  13.  
  14. Pattern pattern = Pattern.compile(regex);
  15. Matcher matcher = pattern.matcher(string);
  16.  
  17. while (matcher.find()) {
  18. for (int i = 1; i <= matcher.groupCount(); i++) {
  19. String grp = matcher.group(i);
  20. if (grp != null) {
  21. System.out.println("Group " + i + ": " + grp);
  22. }
  23. }
  24. }
  25. }
  26. }
Success #stdin #stdout 0.14s 56220KB
stdin
Standard input is empty
stdout
Group 1: 123
Group 2: 1
Group 3: a
Group 3: b