V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
endoffight
V2EX  ›  Python

求助正则表达式牛人帮忙解决一下

  •  
  •   endoffight ·
    phpgao · 2014-08-21 11:12:17 +08:00 · 3505 次点击
    这是一个创建于 3537 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <a href="http://news.hsw.cn/younews/">前一页</a>  <a href="http://news.hsw.cn/system/more/25330000/0000/25330000_00000011.shtml">下一页</a>


    <a href="http://news.hsw.cn/system/more/25330000/0000/25330000_00000012.shtml">下一页</a>


    <a href="http://news.hsw.cn/system/more/25330000/0000/25330000_00000012.shtml">前一页</a>  <a href="http://news.hsw.cn/system/more/25330000/0000/25330000_00000010.shtml">下一页</a>

    需要匹配 下一页 的URL
    我是用的正则如下
    re.compile(r'(?<=[\n|  ])<a href="(http://news.hsw.cn/system/more/.*?)">下一页</a>')
    但是取不到第三种情况的URL,匹配结果是

    http://news.hsw.cn/system/more/25330000/0000/25330000_00000012.shtml">前一页</a>  <a href="http://news.hsw.cn/system/more/25330000/0000/25330000_00000010.shtml

    求指点!
    17 条回复    2014-08-30 07:26:42 +08:00
    bindiry
        1
    bindiry  
       2014-08-21 11:20:12 +08:00   ❤️ 1
    <a href="(http://news.hsw.cn/system/more/((?!<).)+)">下一页</a>

    这样行不行?
    Arrowing
        2
    Arrowing  
       2014-08-21 11:25:23 +08:00   ❤️ 1
    js简单的:
    var s = '<a href="http://news.hsw.cn/system/more/25330000/0000/25330000_00000011.shtml">下一页</a>';

    var r = /<a href="(.*)">下一页/;

    s.match(r)[1];
    alexapollo
        3
    alexapollo  
       2014-08-21 11:47:07 +08:00   ❤️ 1
    <a href="(http://news.hsw.cn/system/more/[^>]*?)">下一页</a>
    endoffight
        4
    endoffight  
    OP
       2014-08-21 11:54:14 +08:00
    @bindiry
    @alexapollo

    感谢啦

    二位的都可以,思路都很好 感谢
    endoffight
        5
    endoffight  
    OP
       2014-08-21 11:54:32 +08:00
    @Arrowing 确实很简单
    Vonex
        6
    Vonex  
       2014-08-21 12:22:54 +08:00
    <a href="[^"]+">[^<]+</a>
    csensix
        7
    csensix  
       2014-08-21 12:56:07 +08:00
    RewriteRule ^/(.*)$ %1/$1

    大伙有空也帮忙解释一下这个正则,apache配置里面的,谢过。
    imn1
        8
    imn1  
       2014-08-21 13:03:34 +08:00   ❤️ 1
    善用字符排除 [^不可能字符],不仅可以匹配更精确,还能加速正则
    xylophone21
        9
    xylophone21  
       2014-08-21 13:03:37 +08:00
    @bindiry
    @alexapollo
    的答案都破不了这种情况:
    <a href="http://news.hsw.cn/system/more/25330000/0000/25330000_00000010.shtml">\n
    下 一 页 < / a >

    即">下一页</a>"
    这几个字符中都可能插入回车,甚至空格都不影响实际使用.
    正则之前把回车空格神马的都去掉吧.

    另外,你确定前面的a标签url都是news.hw.cn开头的?确定不会有别的属性?
    CosWind
        10
    CosWind  
       2014-08-21 13:07:03 +08:00
    @csensix http://stackoverflow.com/questions/6654834/difference-between-1-vs-1-in-htaccess
    貌似要结合RewriteCond 看。Apache的官方文档应该是不错的参考资料。。
    CosWind
        11
    CosWind  
       2014-08-21 13:10:34 +08:00
    CosWind
        12
    CosWind  
       2014-08-21 13:12:28 +08:00
    @endoffight 正则是贪心的,这样肯定匹配的比较多吧。
    CosWind
        13
    CosWind  
       2014-08-21 13:20:25 +08:00
    /"([^"]*)">\s*下\s*一\s*页/这样可以么
    diaoleona
        14
    diaoleona  
       2014-08-21 22:49:35 +08:00
    为何不用xpath
    endoffight
        15
    endoffight  
    OP
       2014-08-24 13:31:37 +08:00
    @diaoleona 你确定Xpath能解决吗?
    WKPlus
        16
    WKPlus  
       2014-08-28 23:47:42 +08:00
    虽然你加了?,表示非贪心的匹配方式,但是误解了贪心的意思,第三行还是会匹配到。

    13楼说的不错,把.*?改为[^\"]*就可以了。

    其实你都用python了,干嘛不用BeautifulSoup?用正则解析html吃力不讨好啊
    endoffight
        17
    endoffight  
    OP
       2014-08-30 07:26:42 +08:00
    @WKPlus 当时追求快,懒得安装bs😁
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   875 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 21:17 · PVG 05:17 · LAX 14:17 · JFK 17:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.