推荐学习书目
› 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
Hello0
V2EX  ›  Python

请教一个关于带分秒的正则表达式问题

  •  
  •   Hello0 · Jul 8, 2021 · 2742 views
    This topic created in 1907 days ago, the information mentioned may be changed or developed.

    目标:想要匹配比如"20 分 15 秒","32 秒"中的 20,15 ; 32

    目前情况:分钟前的用'^(\d+)分'可以正确匹配,但是匹配秒前面的用'(\d+)秒$',在"20 分 15 秒"这种情况下结果总是 None

    问题:想请教下大佬秒前面这种应该怎么写

    4 replies  •  2021-07-08 16:07:00 +08:00
    InDom
        1
    InDom  
       Jul 8, 2021   ❤️ 1
    ^(?:([0-9]+)\s?分)?\s*(?:([0-9]+)\s?秒)?
    no1xsyzy
        2
    no1xsyzy  
       Jul 8, 2021   ❤️ 1
    不要用 re.match,用 re.search

    或者
    ^(?:(?P<minutes>[0-9]+)\s?分)?\s*(?:(?P<seconds>[0-9]+)\s?秒)?
    toaruScar
        3
    toaruScar  
       Jul 8, 2021 via iPhone   ❤️ 1
    中间夹了空格而已,你这种情况去 https://regex101.com 一下就能看出来了
    Hello0
        4
    Hello0  
    OP
       Jul 8, 2021
    @InDom 感谢,原本是没空格的,正文里打错了,去掉\s 后匹配正确
    @no1xsyzy 确实,学习了,感谢
    @toaruScar 正文里打错了,本意想匹配的字符串没带空格,感谢大佬推荐的工具
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2572 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 05:35 · PVG 13:35 · LAX 22:35 · JFK 01:35
    ♥ Do have faith in what you're doing.