fork(1) download
  1. /*
  2.   Separar una URL en sus partes
  3.   https://es.stackoverflow.com/q/131417/127
  4. */
  5.  
  6. import java.util.regex.Pattern;
  7. import java.util.regex.Matcher;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. String prueba = "http://w...content-available-to-author-only...g.org/forum/index.php";
  15.  
  16. //Variables para el regex
  17. final String regex = "^(?:([^:]*):(?://)?)?([^/]*)(/.*)?";
  18. final Pattern pattern = Pattern.compile(regex);
  19. final Matcher matcher = pattern.matcher(prueba);
  20.  
  21. //Ver si coincide el regex
  22. if (matcher.find()) {
  23. //Obtener el texto capturado por cada conjunto de paréntesis
  24. String protocolo = matcher.group(1);
  25. String dominio = matcher.group(2);
  26. String ruta = matcher.group(3);
  27.  
  28. System.out.println("protocolo = " + protocolo);
  29. System.out.println("dominio = " + dominio);
  30. System.out.println("ruta = " + ruta);
  31. }
  32. }
  33. }
Success #stdin #stdout 0.06s 28072KB
stdin
Standard input is empty
stdout
protocolo = http
dominio   = www.devbg.org
ruta      = /forum/index.php