正则表达式,以单斜杠分隔

时间:2018-07-13 06:22:00

标签: java regex

我有以下字符串,

  

https://www.google.com/https://www.yahoo.com/https://facebook.com/

我需要将此字符串拆分为

  

https://www.google.com/

     

https://www.yahoo.com/

     

https://facebook.com/

我尝试过:

String urls[] = firstHalf.split("/*");

有可能吗?

2 个答案:

答案 0 :(得分:6)

我们可以尝试在以下积极方面进行分叉:

(?=https?:)

每当出现新的httphttps协议时,这就会中断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/

Demo

答案 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