Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找以…开头的文本。。。以结尾(短代码样式)_Python_Regex - Fatal编程技术网

Python 查找以…开头的文本。。。以结尾(短代码样式)

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

我想从垃圾文本(基本上是html)中找到所谓的短代码,并用一些东西替换它们

[[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”]