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

2019-04-18 22:35:39 +08:00
 vcent

这段文本

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//-->'

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

1734 次点击
所在节点    Python
4 条回复
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
2019-04-19 07:12:48 +08:00
大哥,参数顺序错了
vcent
2019-04-19 08:30:20 +08:00
@kcnscrew 多谢 已解决
vcent
2019-04-19 08:30:30 +08:00
@GTim 多谢 已解决

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/556592

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX