python .replace()正则表达式

时间:2012-07-13 18:03:50

标签: python regex

我想在“< / html>”后抓一切标记并删除它,但我的代码似乎没有做任何事情。 .replace()不支持正则表达式吗?

z.write(article.replace('</html>.+', '</html>'))

4 个答案:

答案 0 :(得分:210)

没有。 Python中的正则表达式由re模块处理。

article = re.sub(r'(?is)</html>.+', '</html>', article)

答案 1 :(得分:16)

@Ignácio是对的,+ 1,我只是提供更多的例子。

要使用正则表达式替换文本,请使用re.sub函数:

  

sub(pattern,repl,string [,count,flags])

它会将pattern的非常规实例替换为string传递的文本。如果您需要分析匹配以提取有关特定组捕获的信息,那么对于isntance,您可以将函数传递给string参数。 more info here

<强>实施例

>>> import re
>>> re.sub(r'a', 'b', 'banana')
'bbnbnb'

>>> re.sub(r'/\d+', '/{id}', '/andre/23/abobora/43435')
'/andre/{id}/abobora/{id}'

答案 2 :(得分:6)

您可以将re模块用于正则表达式,但正则表达式可能会因您的需要而过度杀伤。我可能会尝试像

这样的东西
z.write(article[:article.index("</html>") + 7]

这比基于正则表达式的解决方案更清晰,更快。

答案 3 :(得分:1)

对于这种特殊情况,如果使用private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { if(this.index <= 5){ int[] R = { 0, 255, 255, 34, 249,255 }; int[] G = { 0, 255, 0, 235, 255 ,153}; int[] B= { 255, 255, 0, 27, 40,51 }; pictureBox1.BackColor = Color.FromArgb(R[this.index], G[this.index], B[this.index]); this.index++; } } 模块过度,那么如何使用re(或split)方法

rsplit

例如,

se='</html>'
z.write(article.split(se)[0]+se)

输出#!/usr/bin/python article='''<html>Larala Ponta Monta </html>Kurimon Waff Moff ''' z=open('out.txt','w') se='</html>' z.write(article.split(se)[0]+se)

out.txt