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

请问正则表达式如何排除一个特定的单词?

  •  
  •   dwjgwsm · Apr 17, 2018 · 7271 views
    This topic created in 3084 days ago, the information mentioned may be changed or developed.

    python 中

    data = r'test TES(1+2) MAX(3+2) AX(4+5)'

    x=r'([^()]*)'

    x=r'[A-Z]+' + x

    b=re.findall(x,data)

    print(b)

    要求输出 ['TES(1+2)','AX(4+5)']

    排除掉前缀为 MAX 的字符串,我尝试前面加(?!MAX) 但不成功

    4 replies  •  2018-04-17 11:28:34 +08:00
    xiangyuecn
        1
    xiangyuecn  
       Apr 17, 2018
    来一发正则表达式

    \b(?!max)\w+?\(.+?\)
    dwjgwsm
        2
    dwjgwsm  
    OP
       Apr 17, 2018
    @xiangyuecn 谢谢~!也可以这么写:
    x=r'\([^()]*\)'

    x=r'\b(?!MAX)[A-Z]+?' + x
    LadyChunsKite
        3
    LadyChunsKite  
       Apr 17, 2018
    酱紫也行:[A-Z]+(?<!MAX)\(.*?\)
    Hopetree
        4
    Hopetree  
       Apr 17, 2018
    \s((?!MAX)[A-Z]+?\(.+?\))

    \s 表示大写字母前面的空格,(?!MAX)表示不匹配这个,[A-Z]+?表示匹配大写字母,\(.+?\)括号和里面的内容
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1414 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:40 · PVG 00:40 · LAX 09:40 · JFK 12:40
    ♥ Do have faith in what you're doing.