[英]Get domain name from URL without java.net in Java
我想知道在GWT中有沒有一種簡單的方法可以從url中獲取主機名,而無需java.net。*;。 由於客戶端不支持此程序包。
Input ejw.example.com/anotherexample?fun=no
output example.com
Input https://www3.example.com/yeteagainanotherexample?fun=miserable
output example.com
com.google.gwt.user.client.Window.Location.getHost()
嘗試將com.google.gwt.regexp.shared.RegExp用於具有Javascript RegExp等功能的正則表達式,以及Javascript String的replace和split方法(可以使用RegExp參數)。
嘗試在正則表達式下面捕獲所有第一個點和第一個正斜杠之間的所有內容,並將其獲取到匹配索引1。
https*://\w+\.(.*?)/
樣例代碼:
String url="https://www3.example.com/yeteagainanotherexample?fun=miserable";
System.out.println(RegExp.compile("https*://\\w+\\.(.*?)/").exec(url).getGroup(1));
注意:我的regex不好,所以請根據需要更改regex模式。 進一步了解
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.