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

关于正则的问题

  •  
  •   Zioyi · 2017-08-02 10:45:26 +08:00 · 2273 次点击
    这是一个创建于 2451 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如果有一个字符串 a=‘ bbbbaab ’ 我想要同时正则出‘ bb ’和‘ baab ’,应该怎么实现?

    7 条回复    2017-08-02 17:18:01 +08:00
    chinvo
        1
    chinvo  
       2017-08-02 10:51:49 +08:00   ❤️ 1
    虽然理论上 baab|bb 可以实现

    不过我刚刚做了个小测试,发现会匹配两个 bb,baab 没有被匹配

    如果你的 bb 一定是位于头部的,可以用

    baab|^bb
    knightdf
        2
    knightdf  
       2017-08-02 11:13:49 +08:00   ❤️ 1
    baab|bb(?!aab)
    如果你想优先匹配 baab 的话:baab|(?<!baa)bb(?!aab)
    araraloren
        3
    araraloren  
       2017-08-02 11:46:15 +08:00
    ~~ 你这是想怎么匹配 ,bb 出现在字符串里的多个位置
    Yourshell
        4
    Yourshell  
       2017-08-02 12:52:02 +08:00   ❤️ 1
    (bb?)(baab)
    用 search 才行
    toan
        5
    toan  
       2017-08-02 15:20:21 +08:00   ❤️ 1
    b(aa)?b(?!aab)
    JerryV2
        6
    JerryV2  
       2017-08-02 16:32:49 +08:00   ❤️ 1
    b = re.match('(bb).*(baab)', a)
    b.group(1)
    b.group(2)
    Zioyi
        7
    Zioyi  
    OP
       2017-08-02 17:18:01 +08:00
    感谢各位大神给的思路
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4706 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 01:09 · PVG 09:09 · LAX 18:09 · JFK 21:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.