2015-10-16 58 views
-3

我想通过正则表达式找到网页上的链接(保存在字符串变量中)。特别由标签定义:“<a href=”link”></a>”。 (从<a href=开始,结束</a>)应该如何看起来像这样的正则表达式,我应该输入什么?领域。 TIA)通过正则表达式查找链接 - Java

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


public class Main { 
public static void main(String[] args) { 

    String sourceOfHtml = "Some html code of webpage with links"; 


    regexChecker("???", sourceOfHtml); 


} 

public static void regexChecker(String theRegex, String str2check){ 

    Pattern checkRegex = Pattern.compile(theRegex); 

    Matcher regexmatcher = checkRegex.matcher(str2check); 

    while(regexmatcher.find()){ 
     if(regexmatcher.group().length()!=0){ 
      System.out.println(regexmatcher.group().trim()); 

     } 


    } 
} 

}

回答

0

你可以试试这个正则表达式:)

/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ 
1

不要使用正则表达式这一点。 Do use an HTML parser.

Document document = Jsoup.parse(sourceOfHtml); 
Elements links = document.select("a[href]");