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

关于 Python 正则求助一下

  •  
  •   gdw1986 · 2020-07-27 16:32:36 +08:00 · 1450 次点击
    这是一个创建于 1340 天前的主题,其中的信息可能已经有所发展或是发生改变。
    很简单的问题,难住我了,就是想在同一行里面去匹配多个字符串,但是死活报错,代码如下:
    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 条回复    2020-07-27 16:40:00 +08:00
    ShuoHui
        1
    ShuoHui  
       2020-07-27 16:36:24 +08:00 via iPhone
    两个字符串加括号试试看
    gdw1986
        2
    gdw1986  
    OP
       2020-07-27 16:38:11 +08:00
    @ShuoHui 试过了,整体括起来和分开括起来都不行
    j0hnj
        3
    j0hnj  
       2020-07-27 16:38:24 +08:00   ❤️ 1
    exlude_keyword = re.search('爱情'|'承诺', text, re.I) => exlude_keyword = re.search('爱情|承诺', text, re.I)
    gdw1986
        4
    gdw1986  
    OP
       2020-07-27 16:40:00 +08:00
    @j0hnj 擦,居然这样的,多谢多谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2998 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:50 · PVG 22:50 · LAX 07:50 · JFK 10:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.