簡體   English   中英

從字符串中提取網址

[英]Extract a url from a string

我知道這個問題重復了很多次,但是我找不到正確的答案。

如果我有一串網址,例如:

“ www.google.comwww.yahoo.comwww.ebay.com”(假設鏈接之間沒有空格)

我想分別提取每個像並將它們放在數組中。 我試圖像這樣使用正則表達式:

    String[] sp= parts.split("\\www");
    System.out.println(parts[0]);

這沒用! 任何提示表示贊賞

正則表達式

(www\.((?!www\.).)*)

正則表達式可視化

Debuggex演示


描述

選項:不區分大小寫

Match the regular expression below and capture its match into backreference number 1 «(www\.((?!www\.).)*)»
    Match the characters “www” literally «www»
    Match the character “.” literally «\.»
    Match the regular expression below and capture its match into backreference number 2 «((?!www\.).)*»
        Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
        Note: You repeated the capturing group itself.  The group will capture only the last iteration.  Put a capturing group around the repeated group to capture all iterations. «*»
        Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!www\.)»
        Match the characters “www” literally «www»
        Match the character “.” literally «\.»
    Match any single character that is not a line break character «.»

Java的

try {
    String subjectString = "www.google.comwww.yahoo.comwww.ebay.com";
    String[] splitArray = subjectString.split("(?i)(www\\.((?!www\\.).)*)");
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

您也可以只使用基本的字符串方法將comwww分解為com www ,然后在空格處簡單地分割:

    String urlString = "www.google.comwww.yahoo.comwww.ebay.com";
    String[] urlArray = urlString.replaceAll(".comwww.", ".com www.").split(" ");
    System.out.println(Arrays.toString(urlArray)); // [www.google.com, www.yahoo.com, www.ebay.com]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM