2014-01-16 21 views
2

之間取<>字符串我有一個URL鏈接如何用Java

Link=<http://mysample.link.com/first/12/sample?page=1234-asdf>;rel="next" 

我想<之間取家居>,並得到結果作爲http://mysample.link.com/first/12/sample?page=1234-asdf

目前我使用String.substring ()像 finalString=sample.subString(sample.indexOf("<"),sample.indexOf(">"));

但我不認爲這是最好的辦法。有人可以告訴我如何使用正則表達式獲取結果字符串。

回答

1

我會用這一個:(?<=Link=<).+?(?=>)只捕捉鏈接 「標籤」 實際的URL。

1

以下內容適用於您。

String s = "Link=<http://mysample.link.com/first/12/sample?page=1234-asdf>;rel=\"next\""; 
Pattern p = Pattern.compile("<([^>]+)>"); 
Matcher m = p.matcher(s); 
while (m.find()) { 
    System.out.println(m.group(1)); 
} 

// "http://mysample.link.com/first/12/sample?page=1234-asdf"