如何在Java項目中的鏈接地址怎么利用正則表達式獲取

這篇文章給大家介紹如何在Java項目中的鏈接地址怎么利用正則表達式獲取,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

千山網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站開發(fā)等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司公司2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。

1、正則表達式中Matcher中find()方法的應(yīng)用。

2、String對象中的 replaceAll(String regex,String replacement) 方法的使用。通過這個方法去除了不必要的字符串,從而得到了需要的網(wǎng)址和鏈接文字

例1.超簡單的

String content = "<a href="URL" rel="external nofollow" >";
String pattern= "href="([^" rel="external nofollow" ]*)"";
Pattern p = Pattern.compile(pattern, 2 | Pattern.DOTALL);
Matcher m = p.matcher(content);
if(m.find()) {
   System.out.println("url="+m.group(1));
}

例2.上面只能獲取帶有雙“號的a標題中的url,下面我們加以改進可以獲取任何狀態(tài)下的a標題url

package com.gong.example;
import Java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Simple {
 public static void main(String[] args){
 String input="<a  href = "https://www.jb51.net" target="_blank" >www.jb51.net</a>" +
 "<a href = 'http://www.163.com' target='_blank' >www.163.com</a> " +
 "<a href=http://www.yahoo.com target=_blank >www.yahoo.com</a>";
 String patternString = "\s*(&#63;i)href\s*=\s*("([^"]*")|'[^']*'|([^'">\s]+))"; //href
 Pattern pattern = Pattern.compile(patternString,
  Pattern.CASE_INSENSITIVE);
 Matcher matcher = pattern.matcher(input);
 while (matcher.find()) {
  String link=matcher.group();
  System.out.println(link);
  link=link.replaceAll("href\s*=\s*(['|"]*)", "");
  System.out.println("--"+link);
  link=link.replaceAll("['|"]", "");
  System.out.println("---"+link);
 }
 }
}

例3.我們還可以利用它進行升級獲取 獲取網(wǎng)址和鏈接文字哦。

/*
   功能說明:分析字符串s,提取s里面的超鏈接和鏈接文字
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegTest
{
  public static void main(String[] args)
  {
    //String s="<p id=km>&nbsp;<a href=http://down.yourweb.com>空間</a>&nbsp;|&nbsp;<a ";
    String s="</p><p style=height:14px><a href=http://mb.yourweb.com>企業(yè)推廣</a> | <a href=http://code.yourweb.com>搜索風云榜</a> | <a href=/home.html>關(guān)于百度</a> | <a href=http://www.yourweb.com>About Baidu</a></p><p id=b>&copy;2008 Baidu <a href=http://www.yourweb.com>使用百度前必讀</a> <a href=http://www.miibeian.gov.cn target=_blank>京ICP證03xxxx號</a> <a href=https://www.jb51.net><img src=/get_pic/2013/11/22/20131122031447947.gif></a></p></center></body></html><!--543ff95f18f36b11-->";
     String regex="<a.*&#63;/a>";
    //String regex = "<a.*>(.*)</a>";
    Pattern pt=Pattern.compile(regex);
    Matcher mt=pt.matcher(s);
    while(mt.find())
    {
       System.out.println(mt.group());
       System.out.println();
       String s2=">.*&#63;</a>";//標題部分
       String s3="href=.*&#63;>";
       Pattern pt2=Pattern.compile(s2);
       Matcher mt2=pt2.matcher(mt.group());
       while(mt2.find())
       {
        System.out.println("標題:"+mt2.group().replaceAll(">|</a>",""));
       }
       Pattern pt3=Pattern.compile(s3);
       Matcher mt3=pt3.matcher(mt.group());
       while(mt3.find())
       {
        System.out.println("網(wǎng)址:"+mt3.group().replaceAll("href=|>",""));
       }
    }
  }
}

關(guān)于如何在Java項目中的鏈接地址怎么利用正則表達式獲取就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)頁名稱:如何在Java項目中的鏈接地址怎么利用正則表達式獲取
轉(zhuǎn)載源于:http://muchs.cn/article36/johosg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管網(wǎng)站制作、建站公司、網(wǎng)站收錄、做網(wǎng)站小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)