最近在項(xiàng)目中,我希望能通過(guò)grep實(shí)現(xiàn)從一個(gè)html頁(yè)面中檢索出所有的超鏈接,比如下面的一段代碼
<tr class=rb><td class=pl><a href=mail.htm>郵 箱</a></td><td><a href=http://mail.163.com/>163郵箱</a><a href="? <a href=http://mail.sina.com.cn/>新浪郵箱</a> <a href=http://mail.qq.com/>QQ郵箱</a> <a href=http://www.hotmail.com/>Hotmail</a></td><td><a href=mail.htm>更多 »</a></td></tr>
<tr class=ry><td class=pl><a href=wangmei.htm>視 頻</a></td><td><a href=http://www.youku.com/>優(yōu)酷網(wǎng)</a> <a href="結(jié)果如下:<tr class=rb><td class=pl><a href=mail.htm>郵 箱</a></td><td><a href=http://mail.163.com/>163郵箱</a> <a href=" 因?yàn)檫@種模式是貪婪匹配模式。我希望能用非貪婪模式,來(lái)進(jìn)行匹配,方法是通過(guò)在*修飾副后面添加\?,修改如下: C:\tmp>grep -ior "href=.*\?\/>" a.txt結(jié)果如下:href=mail.htm>郵 箱</a></td><td><a href=http://mail.163.com/>163郵箱</a> <a href="http://cn.mail.yahoo.com/?id=40014" class="greenfont">雅虎郵箱</a> <a href=http://www.126.com/>126郵箱</a> <a href=http://mail.sina.com.cn/>新浪郵箱</a> <a href=http://mail.qq.com/>QQ郵箱</a> <a href=http://www.hotmail.com/> 我期望的結(jié)果如下: href=mail.htmhref=http://mail.163.com/href=href=http://www.126.com/href=http://mail.sina.com.cn/href=http://mail.qq.com/href=http://www.hotmail.com/href=mail.htm不知道如何實(shí)現(xiàn)。如果您有解決方案,請(qǐng)多多指導(dǎo)。先謝了。 |----------------------------------------------------------------------------------------| 版權(quán)聲明 版權(quán)所有 @zhyiwww 引用請(qǐng)注明來(lái)源 http://www.tkk7.com/zhyiwww |----------------------------------------------------------------------------------------| posted on 2008-09-26 13:25 zhyiwww 閱讀(2998) 評(píng)論(1) 編輯 收藏 所屬分類(lèi): linux
因?yàn)檫@種模式是貪婪匹配模式。我希望能用非貪婪模式,來(lái)進(jìn)行匹配,方法是通過(guò)在*修飾副后面添加\?,修改如下:
C:\tmp>grep -ior "href=.*\?\/>" a.txt結(jié)果如下:href=mail.htm>郵 箱</a></td><td><a href=http://mail.163.com/>163郵箱</a> <a href="http://cn.mail.yahoo.com/?id=40014" class="greenfont">雅虎郵箱</a> <a href=http://www.126.com/>126郵箱</a> <a href=http://mail.sina.com.cn/>新浪郵箱</a> <a href=http://mail.qq.com/>QQ郵箱</a> <a href=http://www.hotmail.com/>
我期望的結(jié)果如下:
href=mail.htmhref=http://mail.163.com/href=href=http://www.126.com/href=http://mail.sina.com.cn/href=http://mail.qq.com/href=http://www.hotmail.com/href=mail.htm不知道如何實(shí)現(xiàn)。如果您有解決方案,請(qǐng)多多指導(dǎo)。先謝了。