我试图做一个抓取'</html>'标签后的一切并删除它,但我的代码似乎没有做任何事情。.replace()不支持正则表达式吗?

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

当前回答

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

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

一般来说:

str_output = re.sub(regex_search_term, regex_replacement, str_input)

其他回答

对于这种特殊的情况,如果使用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为

<html>Larala
Ponta Monta 
</html>

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

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

一般来说:

str_output = re.sub(regex_search_term, regex_replacement, str_input)

你可以为正则表达式使用re模块,但是正则表达式对于你想要的东西来说可能是多余的。我可能会试试

z.write(article[:article.index("</html>") + 7]

这比基于正则表达式的解决方案要干净得多,而且应该快得多。

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

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

它将用作为字符串传递的文本替换模式的非everlaping实例。例如,如果需要分析匹配以提取关于特定组捕获的信息,可以将函数传递给字符串参数。更多信息请点击这里。

例子

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

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