2 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用java.net.URL類進(jìn)行解析
URL url = new URL("https://google.com:443/search");
System.out.println(url.getProtocol()); // https
System.out.println(url.getHost()); // google.com
System.out.println(url.getPort()); // 443

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
有什么特別的理由在你的情況下使用正則表達(dá)式嗎?如果不是,您應(yīng)該使用URL來解析字符串。
URL url = new URL("http://hq.dev.test.domain:8080/ip/CreateRegex");
System.out.println("[Protocol]=" + url.getProtocol());
System.out.println("[Hostname]=" + url.getHost());
System.out.println("[Port]=" + url.getPort());
為了完整起見,您將如何使用正則表達(dá)式進(jìn)行處理:
Pattern regex = Pattern.compile("^(\\w+)://([^:/]*)(?::(\\d+))?.*");
Matcher regexMatcher = regex.matcher("http://hq.dev.test.domain:8080/ip/CreateRegex");
if (regexMatcher.find()) {
System.out.println("[Protocol]=" + regexMatcher.group(1));
System.out.println("[Hostname]=" + regexMatcher.group(2));
System.out.println("[Port]=" + regexMatcher.group(3));
}
添加回答
舉報(bào)