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

Python re.findall 匹配的到 re.sub 却替换不掉呢?

  •  
  •   vcent · 2019-04-18 22:35:39 +08:00 · 1703 次点击
    这是一个创建于 1805 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这段文本

    In [6]: re.findall("<!--.*-->",aa,re.S|re.M)   #这样匹配的到
    Out[6]: ['<!--\ngoogle_ad_client = "pub-7133395778201029";\ngoogle_ad_width = 468;\ngoogle_ad_height = 60;\ngoogle_ad_format = "468x60_as";\ngoogle_ad_type = "image";\ngoogle_ad_channel = "";\n//-->']
    
    In [8]: re.sub("<!--.*-->","",aa,re.S|re.M)  #这样替换不掉呢
    Out[8]: '<!--\ngoogle_ad_client = "pub-7133395778201029";\ngoogle_ad_width = 468;\ngoogle_ad_height = 60;\ngoogle_ad_format = "468x60_as";\ngoogle_ad_type = "image";\ngoogle_ad_channel = "";\n//-->'
    

    一直是这么写的没毛病 啊 为啥这次不行了呢

    4 条回复    2019-04-19 08:30:30 +08:00
    kcnscrew
        1
    kcnscrew  
       2019-04-18 23:33:49 +08:00
    def findall(pattern, string, flags=0)
    def sub(pattern, repl, string, count=0, flags=0)
    GTim
        2
    GTim  
       2019-04-19 07:12:48 +08:00 via iPhone
    大哥,参数顺序错了
    vcent
        3
    vcent  
    OP
       2019-04-19 08:30:20 +08:00
    @kcnscrew 多谢 已解决
    vcent
        4
    vcent  
    OP
       2019-04-19 08:30:30 +08:00
    @GTim 多谢 已解决
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3943 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:22 · PVG 18:22 · LAX 03:22 · JFK 06:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.