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
lucker6666
V2EX  ›  Python

python的正则表达式入门求助

  •  
  •   lucker6666 · 2013-11-18 22:21:35 +08:00 · 3651 次点击
    这是一个创建于 3811 天前的主题,其中的信息可能已经有所发展或是发生改变。
    初学python 遇到正则表达式的难题 各位大虾能推荐下如何入门么

    http://*259

    诸如此类网址末尾是数字 怎么把它从网页里提取出来?
    第 1 条附言  ·  2013-11-19 11:41:18 +08:00
    谢谢各位的帮助 有点心得了
    10 条回复    1970-01-01 08:00:00 +08:00
    yxjxx
        2
    yxjxx  
       2013-11-18 23:21:40 +08:00   ❤️ 1
    我也刚学python不久,写过一篇笔记. http://yxjxx.me/regular-expression
    mengzhuo
        3
    mengzhuo  
       2013-11-18 23:47:07 +08:00   ❤️ 1
    首先网页就不要用正则提取内容,BS4是你的好伙伴
    然后提取的所有链接再用正则匹配

    https?:\/\/([\d\.]+)\/
    Perry
        4
    Perry  
       2013-11-19 00:29:30 +08:00   ❤️ 1
    关于入门:
    入门正则可以不用书
    几分钟的入门:http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/
    cheatsheet:http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/
    然后发挥你的想象力自己写并验证:http://rubular.com
    LetFoxRun
        5
    LetFoxRun  
       2013-11-19 01:01:42 +08:00 via Android   ❤️ 1
    @yxjxx

    博客里的 intersting 是不是打错了,还是故意这么写的?
    sandtears
        6
    sandtears  
       2013-11-19 03:09:35 +08:00   ❤️ 1
    import re
    tmpRe = re.compile(r"^http://.*?(\d+)$")
    tmpNum = tmpRe.match(url).groups()[0]

    此时tmp即为str类型的数字
    clino
        7
    clino  
       2013-11-19 08:54:35 +08:00   ❤️ 1
    建议装一个 kodos ,是一个正则的调试集成环境
    lixm
        8
    lixm  
       2013-11-19 09:23:22 +08:00
    html页面为什么不用xml解析而要去用正则呢?
    yxjxx
        9
    yxjxx  
       2013-11-19 10:01:15 +08:00
    @LetFoxRun 囧,打错了!感谢指出!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5771 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:23 · PVG 14:23 · LAX 23:23 · JFK 02:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.