推荐学习书目
› 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 · Apr 18, 2019 · 2911 views
    This topic created in 2721 days ago, the information mentioned may be changed or developed.

    这段文本

    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 replies  •  2019-04-19 08:30:30 +08:00
    kcnscrew
        1
    kcnscrew  
       Apr 18, 2019
    def findall(pattern, string, flags=0)
    def sub(pattern, repl, string, count=0, flags=0)
    yemoluo
        2
    yemoluo  
       Apr 19, 2019 via iPhone
    大哥,参数顺序错了
    vcent
        3
    vcent  
    OP
       Apr 19, 2019
    @kcnscrew 多谢 已解决
    vcent
        4
    vcent  
    OP
       Apr 19, 2019
    @GTim 多谢 已解决
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2605 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 15:27 · PVG 23:27 · LAX 08:27 · JFK 11:27
    ♥ Do have faith in what you're doing.