python用正则表达式进行文本替换

import re
# 程序功能:要将所有

..

中的文本替换掉 key = r"

hello world

hello wjs

" # 这段是你要匹配的文本 p1 = r"(?<=

).+?(?=

)" # 这是我们写的正则表达式规则 pattern1 = re.compile(p1) # 我们在编译这段正则表达式 print(pattern1.findall(key)) # 查看下匹配到什么 newKey = re.sub(p1, "替换成的文本", key) print("原文本:"+key) print("新文本:"+newKey)

输出:

['hello world', 'hello wjs']
原文本:

hello world

hello wjs

新文本:

替换成的文本

替换成的文本

 

你可能感兴趣的:(python,正则表达式,文本替换)