2013-03-23 66 views
0

我有以下方法:如何解析來自HTML標籤鏈接地址

public void parse(){ 
    String x = "<p><a href=\"http://WWW.xxxx.COM\" class=\"url\" target=\"_blank\">Website for xxxx</a></p>"; 
    int start = 0; 
    int end = 0; 
    for (int i = 0; i < x.length(); i++){ 
     start++; 
     if (x.charAt(i) == '\"'){ 
      start = i; 
     }    
    } 
    System.out.println(x.substring(start)); 
} 

如何從字符串中刪除標記,所以我可以爲最終結果: www.xxxx.com

+0

你想www.xxxx.com只?或與http – stinepike 2013-03-23 05:15:24

+0

是的,如果可能的話沒有http – coco 2013-03-23 05:16:15

+1

你還想等待答案時收到一杯溫暖的咖啡嗎?哦,你也可以要求一個cookie。 – 2013-03-23 05:18:41

回答

0

使用替代像方法這個:

String x = "<p><a href=\"http://WWW.xxxx.COM\" class=\"url\" target=\"_blank\">Website for xxxx</a></p>"; 
    String result = x.replaceAll(".*href=\"http://([^\"]*)\".*", "$1"); 

希望它適合你。

+0

是的,非常感謝。你能解釋一下「。* href = \」http://([^ \「] *)的語法嗎?」「*」,「$ 1」 – coco 2013-03-23 05:26:32

+0

。* href = \「http://([^ \ *]匹配href =「http:// [^ \」] *之前的字符數匹配除/之外的任何字符「括號()告訴ajva它的一個子組(由$ 1表示,即在這裏匹配的部分將被存儲爲$ 1替換 \「匹配」 。*匹配任意數量的字符(任何類型的字符) *實際上是指任意數量的字符 。表示任何字符 。*表示任意數量的任何類型的字符 – 2013-03-23 06:21:09

0

如果你不想使用正則表達式,你也可以這樣做。

String x = "<p><a href=\"http://WWW.xxxx.COM\" class=\"url\" target=\"_blank\">Website for xxxx</a></p>"; 
    x = x.substring(x.indexOf("/") + 2); // or x = x.substring(x.indexOf("W")); 
    x = x.substring(0, x.indexOf("\"")); 
    System.out.println(x); 
0

你可以做到這一點,如下圖所示:

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class RegexTest { 

    public static void main(String[] args) { 
     String strYourText = "<p><a href=\"http://WWW.xxxx.COM\" class=\"url\" target=\"_blank\">Website for xxxx</a></p>"; 
     Matcher matcher = Pattern.compile("href=\"(.*?)\"").matcher(strYourText); 
     while (matcher.find()) { 
      System.out.println(matcher.group(1)); 
     } 
    } 
}