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

请教一个正则问题,懒惰匹配如何匹配多个内容

  •  
  •   wonder1z · 2018-11-22 15:37:18 +08:00 · 1763 次点击
    这是一个创建于 1974 天前的主题,其中的信息可能已经有所发展或是发生改变。
    str = 'asd(hello)asd(world)'
    s = re.search(r'(?<=\().*?(?=\))',str1)
    print(s)
    
    <re.Match object; span=(4, 9), match='hello'>
    

    只能匹配出第一个 hello,如何匹配出所有()里的内容呢

    6 条回复    2018-11-22 16:14:11 +08:00
    masker
        1
    masker  
       2018-11-22 15:40:44 +08:00 via Android
    \w+\((.*?)\){1,}
    sess222
        2
    sess222  
       2018-11-22 15:41:55 +08:00   ❤️ 1
    懒惰匹配是虾米?
    re.findall('\((.*?)\)', str1)
    wonder1z
        3
    wonder1z  
    OP
       2018-11-22 15:47:25 +08:00
    @masker 老铁 你这个不对啊
    wonder1z
        4
    wonder1z  
    OP
       2018-11-22 15:48:57 +08:00
    @sess222 这个也不对的,懒惰就是尽可能少的重复
    wonder1z
        5
    wonder1z  
    OP
       2018-11-22 15:51:55 +08:00
    @sess222 sorry 搞错了,你这个可以的
    Atukey
        6
    Atukey  
       2018-11-22 16:14:11 +08:00
    (?<=\()\w*(?=\))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1229 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:26 · PVG 07:26 · LAX 16:26 · JFK 19:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.