zhyiwww
          用平實的筆,記錄編程路上的點點滴滴………
          posts - 536,comments - 394,trackbacks - 0

          最近在項目中,我希望能通過grep實現從一個html頁面中檢索出所有的超鏈接,
          比如下面的一段代碼

          <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>更多&nbsp;&raquo;</a></td></tr>

          <tr class=ry><td class=pl><a href=wangmei.htm>視 頻</a></td><td><a href=http://www.youku.com/>優酷網</a>  <a href="結果如下:
          <tr class=rb><td class=pl><a href=mail.htm>郵 箱</a></td><td><a href=http://mail.163.com/>163郵箱</a>   <a href="

          因為這種模式是貪婪匹配模式。我希望能用非貪婪模式,來進行匹配,方法是通過在*修飾副后面添加\?,修改如下:

          C:\tmp>grep -ior "href=.*\?\/>" a.txt
          結果如下:
          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/>

          我期望的結果如下:

          href=mail.htm
          href=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
          不知道如何實現。如果您有解決方案,請多多指導。先謝了。



          |----------------------------------------------------------------------------------------|
                                     版權聲明  版權所有 @zhyiwww
                      引用請注明來源 http://www.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2008-09-26 13:25 zhyiwww 閱讀(3001) 評論(1)  編輯  收藏 所屬分類: linux

          FeedBack:
          # re: grep的非貪婪模式
          2008-09-26 13:54 | zhyiwww
          我又用了下面的方法
          grep -ior "href=[a-z1-9A-Z\?/:\.]*" b.txt
          結果是:
          href=mail.htm
          href=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
          但是這種方法沒有使用正則表達式的非貪婪模式。
          不知道如何使用非貪婪匹配模式來解決此問題。  回復  更多評論
            
          主站蜘蛛池模板: 桂阳县| 高密市| 商水县| 鄢陵县| 稻城县| 包头市| 仙居县| 西贡区| 同江市| 盈江县| 东丽区| 阳泉市| 荣成市| 莒南县| 金寨县| 高淳县| 余江县| 巴林右旗| 平湖市| 肇东市| 镇坪县| 梁平县| 海阳市| 科技| 德昌县| 宁都县| 博乐市| 罗城| 名山县| 方正县| 四会市| 昆山市| 汉源县| 遂昌县| 丹棱县| 合阳县| 马公市| 太原市| 漠河县| 明光市| 宁陵县|