溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2020-12-02 15:16:35 來源:億速云 閱讀:151 作者:Leah 欄目:編程語言

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

1、正則表達(dá)式中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標(biāo)題中的url,下面我們加以改進(jìn)可以獲取任何狀態(tài)下的a標(biāo)題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.我們還可以利用它進(jìn)行升級獲取 獲取網(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>搜索風(fēng)云榜</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>";//標(biāo)題部分
       String s3="href=.*&#63;>";
       Pattern pt2=Pattern.compile(s2);
       Matcher mt2=pt2.matcher(mt.group());
       while(mt2.find())
       {
        System.out.println("標(biāo)題:"+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項目中的鏈接地址怎么利用正則表達(dá)式獲取就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI