Java使用正則表達式匹配獲取鏈接地址的方法示例
本文實例講述了Java使用正則表達式匹配獲取鏈接地址的方法。分享給大家供大家參考,具體如下:
獲取頁面中字符串的url地址我們都會使用正則表達式來匹配獲取了,下面我來給大家總結(jié)幾個匹配獲取鏈接地址示例。
1、正則表達式中Matcher中find()方法的應用。
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 style=" " href = "http://www.dbjr.com.cn" target="_blank" >www.dbjr.com.cn</a>" +
"<a target='_blank' >www.163.com</a> " +
"<a href=http://www.yahoo.com target=_blank >www.yahoo.com</a>";
String patternString = "\s*(?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> <a href=http://down.yourweb.com>空間</a> | <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>©2008 Baidu <a href=http://www.yourweb.com>使用百度前必讀</a> <a href=http://www.miibeian.gov.cn target=_blank>京ICP證03xxxx號</a> <a href=http://www.dbjr.com.cn><img src=/get_pic/2013/11/22/20131122031447947.gif></a></p></center></body></html><!--543ff95f18f36b11-->";
String regex="<a.*?/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=">.*?</a>";//標題部分
String s3="href=.*?>";
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=|>",""));
}
}
}
}
PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java正則表達式技巧大全》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Idea2023創(chuàng)建springboot不能選擇java8的解決方法(最新推薦)
在idea2023版本創(chuàng)建springboot的過程中,選擇java版本時發(fā)現(xiàn)沒有java8版本,只有java17和java20,遇到這樣的問題如何解決呢,下面小編給大家分享Idea2023創(chuàng)建springboot不能選擇java8的解決方法,感興趣的朋友一起看看吧2024-01-01
Spring?Cache+Redis緩存數(shù)據(jù)的實現(xiàn)示例
本文主要介紹了Spring?Cache+Redis緩存數(shù)據(jù),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
spring-cloud-stream結(jié)合kafka使用詳解
這篇文章主要介紹了spring-cloud-stream結(jié)合kafka使用詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
SpringBoot后端接收參數(shù)優(yōu)化代碼示例(統(tǒng)一處理前端參數(shù))
使用Spring Boot開發(fā)API的時候,讀取請求參數(shù)是服務端編碼中最基本的一項操作,下面這篇文章主要給大家介紹了關(guān)于SpringBoot后端接收參數(shù)優(yōu)化(統(tǒng)一處理前端參數(shù))的相關(guān)資料,需要的朋友可以參考下2024-07-07
IDEA中的yml文件與properties互相轉(zhuǎn)換
這篇文章主要介紹了IDEA中的yml文件與properties互相轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Mybatis配置映射文件中parameterType的用法講解
這篇文章主要介紹了Mybatis配置映射文件中parameterType的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09

