文字列でプロトコル部分文字列 ("http[s]") のインデックスを検索するか、単純なPattern
("website[0-9]" ヘッドに一致する場合のみ、URL には適用しない) を使用することができます。 .
を使った解決方法をご紹介し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]