You don't have to (and probably shouldn't) use regex here. Instead use classes defined to handle things like this. You can use for example URL
, URI
, File
classes like
String address = "http://somedomain.someextention/somefolder/.../someotherfolder/somepage";
URL url = new URL(address);
File file = new File(url.getPath());
System.out.println(url.getHost());
System.out.println(url.getPath());
System.out.println(file.getName());
Outpit:
somedomain.someextention
/somefolder/.../someotherfolder/somepage
somepage
Now you can need to get rid of /
at start of path to your resource. You can use substring(1)
here if resource starts with /
.
But if you really must use regex you can try with
^https?://([^/]+)/(.*/([^/]+))$
Now
- group 1 will contain host name,
- group 2 will contain path to resource
- group 3 will contain name of resource