使用Java从Text / String获取特定URL

时间:2013-06-20 13:21:00

标签: java string url

我是Java的新手,我想获得下面文本中的所有网址

WEBSITE1 https://localhost:8080/admin/index.php?page=home
WEBSITE2 https://192.168.0.3:8084/index.php
WEBSITE3 https://192.168.0.5:9090/controller/index.php?page=home
WEBSITE4 https://192.168.0.1:8080/home/index.php?page=forum

我想要的结果是:

https://localhost:8080
https://192.168.0.3:8084
https://192.168.0.5
https://192.168.0.1:8080

我也希望将它存储到链接列表或数组中。 有人可以教我吗? 谢谢

5 个答案:

答案 0 :(得分:1)

这是你如何做到这一点的。我为你做了一个,你做其余的事情:))

try {
            ArrayList<String> urls = new ArrayList<String>();
            URL aURL = new URL("https://localhost:8080/admin/index.php?page=home");
             System.out.println("protocol = " + aURL.getProtocol()+aURL.getHost()+aURL.getPort());
             urls.add(aURL.getProtocol()+aURL.getHost()+aURL.getPort());
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

答案 1 :(得分:0)

假设line代表一行(可能在一个循环中):

//get the index of "https" in the string
int indexOfHTTPS= line.indexOf("https://");
//get the index of the first "/" after the "https"
int indexOfFirstSlashAfterHTTPS= line.indexOf("/", indexOfHTTPS + "https://".length());

//take a string between "https" and the first "/"
String url = line.substring(indexOfHTTPS, indexOfFirstSlashAfterHTTPS);

稍后,将此网址添加到ArrayList<String>

ArrayList<String> urlList= new ArrayList<String>();
urlList.add(url);

答案 2 :(得分:0)

您可以在URL class.

的帮助下完成此操作
 public static void main(String[] args) throws MalformedURLException { 

        String string ="https://192.168.0.5:9090/controller/index.php?page=home";
        URL url= new URL(string);
        String result ="https://"+url.getHost()+":"+url.getPort();
        System.out.println(result);
    }

Output :https://192.168.0.5:9090

答案 3 :(得分:0)

您可以尝试在字符串中找到协议子字符串的索引(“http [s]”),也可以使用简单的Pattern(仅用于匹配“website [0-9]”头部,来申请网址)。

以下是Pattern的解决方案。

String webSite1 = "WEBSITE1 https://localhost:8080/admin/index.php?page=home";
String webSite2 = "WEBSITE2 https://192.168.0.3:8084/index.php";
String webSite3 = "WEBSITE3 https://192.168.0.5:9090/controller/index.php?page=home";
String webSite4 = "WEBSITE4 https://192.168.0.1:8080/home/index.php?page=forum";
ArrayList<URI> uris = new ArrayList<URI>();
Pattern pattern = Pattern.compile("^website\\d+\\s+?(.+)", Pattern.CASE_INSENSITIVE);
Matcher matcher;
matcher = pattern.matcher(webSite1);
if (matcher.find()) {
    try {
        uris.add(new URI(matcher.group(1)));
    }
    catch (URISyntaxException use) {
        use.printStackTrace();
    }
}
matcher = pattern.matcher(webSite2);
if (matcher.find()) {
    try {
        uris.add(new URI(matcher.group(1)));
    }
    catch (URISyntaxException use) {
        use.printStackTrace();
    }
}
matcher = pattern.matcher(webSite3);
if (matcher.find()) {
    try {
        uris.add(new URI(matcher.group(1)));
    }
    catch (URISyntaxException use) {
        use.printStackTrace();
    }
}
matcher = pattern.matcher(webSite4);
if (matcher.find()) {
    try {
        uris.add(new URI(matcher.group(1)));
    }
    catch (URISyntaxException use) {
        use.printStackTrace();
    }
}
System.out.println(uris);

输出:

[https://localhost:8080/admin/index.php?page=home, https://192.168.0.3:8084/index.php, https://192.168.0.5:9090/controller/index.php?page=home, https://192.168.0.1:8080/home/index.php?page=forum]

答案 4 :(得分:0)

使用简单的正则表达式查找从https?://开始的内容,然后将其解压缩到第一个/

Matcher m = Pattern.compile("(https?://[^/]+)").matcher(//
        "WEBSITE1 https://localhost:8080/admin/index.php?page=home\r\n" + //
        "WEBSITE2 https://192.168.0.3:8084/index.php\r\n" + //
        "WEBSITE3 https://192.168.0.5:9090/controller/index.php?page=home\r\n" + //
        "WEBSITE4 https://192.168.0.1:8080/home/index.php?page=forum");
List<String> urls = new ArrayList<String>();
while (m.find()) {
    urls.add(m.group(1));
}
System.out.println(urls);

现在,如果您只想获得WEBSITE.部分,则只需使用以下内容更改正则表达式"(https?://[^/]+)""(.*?)\\s+https?"。其余的代码保持不变。