Last Updated: February 25, 2016
·
745
· terukizm

get domain strings from uri in java

String uri = "http://foo.bar/spam/ham";
String domain = null;
Pattern p = Pattern
.compile("https?:\/\/[-_.!~*'()a-zA-Z0-9;?:\@&=+\$,%#]+\/");
Matcher m = p.matcher(uri);
if (m.find()) {
domain = m.group(0);
}

assertEquals("http://foo.bar/", domain);