0

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
wtsang02
  • 18,603
  • 10
  • 49
  • 67
  • `ejw.example.com`, `www3.example.com` and `example.com` are all different domain names. – Andrei Volgin Jun 20 '14 at 20:00
  • I meant host. And I am looking for a built-in method if it exist. – wtsang02 Jun 20 '14 at 20:04
  • They are different host names as well :), because they can - and often do - resolve to different IP addresses. What you look for in your examples is a second-level domain name, but you have to make sure that this is really what you need. – Andrei Volgin Jun 20 '14 at 20:05
  • Correct. 2nd level domain name. I would write my own method but if there was a built-in class that replaces java.net that would be great. – wtsang02 Jun 20 '14 at 20:13
  • You may find an answer to this question very helpful: http://stackoverflow.com/questions/4452916/need-a-regular-expression-to-capture-second-level-domain-sld – Andrei Volgin Jun 20 '14 at 20:50

2 Answers2

1
com.google.gwt.user.client.Window.Location.getHost()
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
DevMo
  • 21
  • 1
0

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

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Why do you think he needs his own domain name? I think the OP is talking about URLs in general. – Andrei Volgin Jun 20 '14 at 20:07
  • And you did not provide the second-level domain name that the OP is looking for. – Andrei Volgin Jun 20 '14 at 20:08
  • What about `blog.inside.example.co.uk`? Your regex fails. There can be up to 127 dots in domain name. – Andrei Volgin Jun 20 '14 at 20:37
  • @AndreiVolgin Sorry I have very little knowledge about URL domain name. It's just a example. The regex can be modified or simply use String#split() method. – Braj Jun 20 '14 at 20:38
  • String.splt() is also not trivial, because you need to distinguish between `blog.example.com` and `blog.example.com.me" - just to make up an example. – Andrei Volgin Jun 20 '14 at 20:46
  • @AndreiVolgin Sorry I don't know all the combination of the URL pattern. – Braj Jun 20 '14 at 20:48