推荐学习书目
› 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 · Nov 22, 2018 · 2694 views
    This topic created in 2874 days ago, the information mentioned may be changed or developed.
    str = 'asd(hello)asd(world)'
    s = re.search(r'(?<=\().*?(?=\))',str1)
    print(s)
    
    <re.Match object; span=(4, 9), match='hello'>
    

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

    6 replies  •  2018-11-22 16:14:11 +08:00
    masker
        1
    masker  
       Nov 22, 2018 via Android
    \w+\((.*?)\){1,}
    sess222
        2
    sess222  
       Nov 22, 2018   ❤️ 1
    懒惰匹配是虾米?
    re.findall('\((.*?)\)', str1)
    wonder1z
        3
    wonder1z  
    OP
       Nov 22, 2018
    @masker 老铁 你这个不对啊
    wonder1z
        4
    wonder1z  
    OP
       Nov 22, 2018
    @sess222 这个也不对的,懒惰就是尽可能少的重复
    wonder1z
        5
    wonder1z  
    OP
       Nov 22, 2018
    @sess222 sorry 搞错了,你这个可以的
    Atukey
        6
    Atukey  
       Nov 22, 2018
    (?<=\()\w*(?=\))
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   831 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 19:14 · PVG 03:14 · LAX 12:14 · JFK 15:14
    ♥ Do have faith in what you're doing.