Python 查找以…开头的文本。。。以结尾(短代码样式)
我想从垃圾文本(基本上是html)中找到所谓的短代码,并用一些东西替换它们Python 查找以…开头的文本。。。以结尾(短代码样式),python,regex,Python,Regex,我想从垃圾文本(基本上是html)中找到所谓的短代码,并用一些东西替换它们 [[img src="picture.jpg" alt="Description" caption="Caption here..."]] 我知道我可以使用.find,但是我如何告诉python从[[img开始,然后停在“]]?问题是文本中可能有很多内容,甚至是在行中…就像@Sam建议的那样 import re t = 'foobar[[img src="picture.jpg" alt="Description" c
[[img src="picture.jpg" alt="Description" caption="Caption here..."]]
我知道我可以使用.find
,但是我如何告诉python从[[img
开始,然后停在“]]
?问题是文本中可能有很多内容,甚至是在行中…就像@Sam建议的那样
import re
t = 'foobar[[img src="picture.jpg" alt="Description" caption="Caption here..."]]helloworld'
p = r'(\[\[img.*?\"\]\])'
foo = re.findall(p,t)
t = t.replace(foo[0],'bar')
print t
r'\[\[img.*?\]\]'
硬模式:正确匹配字符串,如[[img alt=“Descr]]iption”]