简体   繁体   English

在Java中从没有Java.net的URL获取域名

[英]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.*; 我想知道在GWT中有没有一种简单的方法可以从url中获取主机名,而无需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). 尝试将com.google.gwt.regexp.shared.RegExp用于具有Javascript RegExp等功能的正则表达式,以及Javascript String的replace和split方法(可以使用RegExp参数)。

Try below regex that captures all the everything between first dot and first forward slash and get it at match index 1. 尝试在正则表达式下面捕获所有第一个点和第一个正斜杠之间的所有内容,并将其获取到匹配索引1。

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

Here is demo on debuggex and regexr 这是关于debuggexregexr的演示

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. 注意:我的regex不好,所以请根据需要更改regex模式。 Read more about 进一步了解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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