我有以下字符串,
https://www.google.com/https://www.yahoo.com/https://facebook.com/
我需要将此字符串拆分为
我尝试过:
String urls[] = firstHalf.split("/*");
有可能吗?
答案 0 :(得分:6)
我们可以尝试在以下积极方面进行分叉:
(?=https?:)
每当出现新的http
或https
协议时,这就会中断URL字符串。
String input = "https://www.google.com/https://www.yahoo.com/https://facebook.com/";
String[] urls = input.split("(?=https?:)");
for (String url : urls) {
System.out.println(url);
}
https://www.google.com/
https://www.yahoo.com/
https://facebook.com/
答案 1 :(得分:2)
您也可以尝试以下方法:
String test = "https://www.google.com/https://www.yahoo.com/https://facebook.com/";
String[] urls = test.split("(?<!/)/(?!/)");
for(String s:urls){
System.out.println(s);
}
输出
https://www.google.com
https://www.yahoo.com
https://facebook.com