gpt4 book ai didi

java - 使用 Java 从文本/字符串中获取特定 URL

转载 作者:行者123 更新时间:2023-12-01 14:25:09 25 4
gpt4 key购买 nike

我是 Java 新手,我想获取下面文本中的所有 URL

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

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

最佳答案

这就是你可以做到这一点的方法。我为你做了一个,剩下的你做:)

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

关于java - 使用 Java 从文本/字符串中获取特定 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17214774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com