V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
gdw1986
V2EX  ›  Python

关于 Python 正则求助一下

  •  
  •   gdw1986 · Jul 27, 2020 · 2201 views
    This topic created in 2104 days ago, the information mentioned may be changed or developed.
    很简单的问题,难住我了,就是想在同一行里面去匹配多个字符串,但是死活报错,代码如下:
    import re

    text = '曾经有一份经典的爱情'
    keyword = '爱情'
    exlude_keyword = re.search('爱情'|'承诺', text, re.I)
    print(exlude_keyword)

    报错:
    Traceback (most recent call last):
    File "C:/Users/scndwg/Documents/Viya/aml8.1/AutoFiles/aml/zzz.py", line 6, in <module>
    exlude_keyword = re.search('爱情'|'承诺', text, re.I)
    TypeError: unsupported operand type(s) for |: 'str' and 'str'
    4 replies    2020-07-27 16:40:00 +08:00
    fcmio
        1
    fcmio  
       Jul 27, 2020 via iPhone
    两个字符串加括号试试看
    gdw1986
        2
    gdw1986  
    OP
       Jul 27, 2020
    @ShuoHui 试过了,整体括起来和分开括起来都不行
    j0hnj
        3
    j0hnj  
       Jul 27, 2020   ❤️ 1
    exlude_keyword = re.search('爱情'|'承诺', text, re.I) => exlude_keyword = re.search('爱情|承诺', text, re.I)
    gdw1986
        4
    gdw1986  
    OP
       Jul 27, 2020
    @j0hnj 擦,居然这样的,多谢多谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   923 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 18:48 · PVG 02:48 · LAX 11:48 · JFK 14:48
    ♥ Do have faith in what you're doing.