代码如下:
1
/***/
/**Theregexforsearchlinkwiththetag"a"*/
2 private finalStringA_REGEX="<a.*?/a>";
3 /***/ /**Theregexforsearchurlwiththetag"href"*/
4 private finalStringHREF_REGEX="href=\".*?\"";
5 /***/ /**Thepatternforlinkewiththetag"a"*/
6 private finalPatternA_PATTERN=Pattern.compile(A_REGEX);
7 /***/ /**Thepatternforurlwiththetag"href"*/
8 private finalPatternHREF_PATTERN=Pattern.compile(HREF_REGEX);
9 /***/ /**
10*Geturladdressfromtheurlandthecontentoftheurl
11*@paramurltheurlneedtobegetlinks
12*@paramcontentthecontentofthegivenurl
13*@returnalistwiththeurladdressofthelinks
14*/
15 publicList<String>getLinkList(URLurl,Stringcontent)
16 {
17List<String>linkList=newLinkedList<String>();
18finalMatchera_matcher=A_PATTERN.matcher(content);
19while(a_matcher.find())
20{
21//JUSTFORTEST!
22//System.out.println(a_matcher.group());
23//geturladdress
24finalMatchermyurl=HREF_PATTERN.matcher(a_matcher.group());
25while(myurl.find())
26{
27StringurlAddress=myurl.group().replaceAll("href=|>|\"|\"","");
28if(urlAddress.startsWith("http"))
29{
30linkList.add(urlAddress);
31}
32elseif(urlAddress.startsWith("/")||urlAddress.startsWith("\\"))
33{
34linkList.add(url.getPath()+urlAddress);
35}
36else
37{
38StringfullUrl=url.toString();
39//thelengthoftheurlwithoutthecurrentpage
40intlastSlash=fullUrl.lastIndexOf("/")+1;
41linkList.add(fullUrl.substring(0,lastSlash)+urlAddress);
42}
43}
44}
45returnlinkList;
46}
原文链接:https://www.f2er.com/regex/361004.html2 private finalStringA_REGEX="<a.*?/a>";
3 /***/ /**Theregexforsearchurlwiththetag"href"*/
4 private finalStringHREF_REGEX="href=\".*?\"";
5 /***/ /**Thepatternforlinkewiththetag"a"*/
6 private finalPatternA_PATTERN=Pattern.compile(A_REGEX);
7 /***/ /**Thepatternforurlwiththetag"href"*/
8 private finalPatternHREF_PATTERN=Pattern.compile(HREF_REGEX);
9 /***/ /**
10*Geturladdressfromtheurlandthecontentoftheurl
11*@paramurltheurlneedtobegetlinks
12*@paramcontentthecontentofthegivenurl
13*@returnalistwiththeurladdressofthelinks
14*/
15 publicList<String>getLinkList(URLurl,Stringcontent)
16 {
17List<String>linkList=newLinkedList<String>();
18finalMatchera_matcher=A_PATTERN.matcher(content);
19while(a_matcher.find())
20{
21//JUSTFORTEST!
22//System.out.println(a_matcher.group());
23//geturladdress
24finalMatchermyurl=HREF_PATTERN.matcher(a_matcher.group());
25while(myurl.find())
26{
27StringurlAddress=myurl.group().replaceAll("href=|>|\"|\"","");
28if(urlAddress.startsWith("http"))
29{
30linkList.add(urlAddress);
31}
32elseif(urlAddress.startsWith("/")||urlAddress.startsWith("\\"))
33{
34linkList.add(url.getPath()+urlAddress);
35}
36else
37{
38StringfullUrl=url.toString();
39//thelengthoftheurlwithoutthecurrentpage
40intlastSlash=fullUrl.lastIndexOf("/")+1;
41linkList.add(fullUrl.substring(0,lastSlash)+urlAddress);
42}
43}
44}
45returnlinkList;
46}