我有像http://www.google.com/api/js/example这样的网址,我想在java中使用正则表达式仅提取http:/www.google.com,我该怎么做?
只需使用URL:
public static void main(String[] args) throws Exception {
final URL url = new URL("http://www.google.com/api/js/example");
System.out.println(url.getProtocol() + "://" + url.getHost());
}
Run Code Online (Sandbox Code Playgroud)