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

python 正则怎么获取全部 match 对象?

  •  
  •   vtoexsir · 2016-10-13 19:37:42 +08:00 · 2255 次点击
    这是一个创建于 2753 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import re
    s='aabbaa'
    s 字符串或有 4 个字符 a.
    md=re.search('a',s) #但 search()返回的是 s 字符串中第一匹配到的 match 对象
    fa=re.findall('a',s) #这个findall()找到了所有4个匹配,但返回的是匹配得到的字符串,而不是match对象.
    mds=re.scan('a',s) #我想得到全部 4 个匹配到的 match 对象,但是 python 没有 scan()方法?该怎么办呢?
    如果自己手动实现,是可以的,但我想这是个很常用的功能,python 不会不内置解决方法吧?
    请各位指点!
    2 条回复    2016-10-14 08:58:24 +08:00
    binux
        1
    binux  
       2016-10-13 19:55:30 +08:00   ❤️ 1
    re.finditer
    Cabana
        2
    Cabana  
       2016-10-14 08:58:24 +08:00
    用 pattern find
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   908 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 108ms · UTC 20:01 · PVG 04:01 · LAX 13:01 · JFK 16:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.