Register now and start sharing your code snippets.
-->

Split a URL into protocol, domain, port and URI using regular expressions

Java posted about 1 year ago by christian

   1  // Split URL into protocol, domain, port and URI
   2  Pattern pattern = Pattern.compile("(https?://)([^:^/]*)(:\\d*)?(.*)?");
   3  Matcher matcher = pattern.matcher(url);
   4  
   5  matcher.find();
   6  
   7  String protocol = matcher.group(1);            
   8  String domain   = matcher.group(2);
   9  String port     = matcher.group(3);
  10  String uri      = matcher.group(4);

Tagged regex, java, url