如何使用正则表达式提取HTML链接

本教程介绍了如何使用正则表达式从HTML页面中提取超链接。通过Java示例和TestNG单元测试,展示从`<a>`标签中抓取链接的步骤。
摘要由CSDN通过智能技术生成

在本教程中,我们将向您展示如何从HTML页面提取超链接。 例如,要从以下内容获取链接:

this is text1 <a href='mkyong.com' target='_blank'>hello</a> this is text2...
  1. 首先从“价值” a标签-结果: a href='mkyong.com' target='_blank'
  2. 稍后从上面提取的值中获取“链接” –结果: mkyong.com

1.正则表达式模式

提取标签正则表达式模式

(?i)<a([^>]+)>(.+?)</a>

从标签正则表达式模式中提取链接

\s*(?i)href\s*=\s*(\"([^"]*\")|'[^']*'|([^'">\s]+));

描述

(		#start of group #1
 ?i		#  all checking are case insensive
)		#end of group #1
<a              #start with "<a"
  (		#  start of group #2
    [^>]+	#     anything except (">"), at least one character
   )		#  end of group #2
  >		#     follow by ">"
    (.+?)	#	match anything 
         </a>	#	  end with "</a>
\s*			   #can start with whitespace
  (?i)			   # all checking are case insensive
     href		   #  follow by "href" word
        \s*=\s*		   #   allows spaces on either side of the equal sign,
              (		   #    start of group #1
               "([^"]*")   #      allow string with double quotes e
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值