如何从任何字符串url获取网站名称 [英] How to get name of website from any string url
问题描述
我已经给出了包含任何有效网址的String。
我必须从给定的网址中找到网站的名称。
我也忽略子域。
I have given String which contains any valid url. I have to find only name of website from given url. I have also ignore sub domains.
喜欢
http://www.yahoo.com => yahoo
www.google.co.in => google
http://in.com => in
http://india.gov.in/ => india
https://in.yahoo.com/ => yahoo
http://philotheoristic.tumblr.com/ =>tumblr
http://philotheoristic.tumblr.com/
https://in.movies.yahoo.com/ =>yahoo
如何做到这一点
推荐答案
正则表达式可以帮助您:
Regular expressions may help you:
String str = "www.google.co.in";
String [] res = str.split("(\\.|//)+(?=\\w)");
System.out.println(res[1]);
正则表达式是表示一组字符串的一种方式。该集由与表达式匹配的任何字符串组成。在上面的代码中,用作 split
参数的字符串是匹配的正则表达式:Any。后跟一个字母数字文本或//后跟一个字母数字文本。
所以这些。和//子串是用于分割字符串的分隔符,第一个是网站名称。
A regular expression is a way to represent a set of strings. This set is composed by any string matching the expression. In the code above, the string used as split
argument is the regular expression that matches: Any "." followed by an alphanumeric text OR "//" followed by an alphanumeric text.
So these "." and "//" substrings are the separators used to split the string in parts, being the first one the site name.
在www.google.co.in中,字符串将以这种方式分割: goole,co,在
中。由于解决方案是使用spit数组的第一个元素,结果是: google
。
In "www.google.co.in", the string would be splited this way: goole, co, in
. Since the solution is using the first element of the spit array, the result is: google
.
这篇关于如何从任何字符串url获取网站名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!