简体   繁体   中英

Get domain name from URL without java.net in Java

I would like to know in GWT is there an easy way to get host name from url WITHOUT java.net.*; Since client side doesn't support this package.

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()

Try with com.google.gwt.regexp.shared.RegExp that is used for regular expressions with features like Javascript's RegExp, plus Javascript String's replace and split methods (which can take a RegExp parameter).

Try below regex that captures all the everything between first dot and first forward slash and get it at match index 1.

https*://\w+\.(.*?)/

Here is demo on debuggex and regexr

Sample code:

String url="https://www3.example.com/yeteagainanotherexample?fun=miserable";

System.out.println(RegExp.compile("https*://\\w+\\.(.*?)/").exec(url).getGroup(1));

Note: I am not good in regex so please change the regex pattern as per your need. Read more about

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM