我在python中写作,让我们说我的文件看起来像这样:
aaa
aaa "word" --sdsdrrr2 --sds {
test test
}
aaa "word2" --sdsdd sdsd {
fffsd
ssss
}
aaa "word3" -sdksdld sdsd
{
sdsdd
sdsddd
sdsdddd }
现在我想解析" word2"的输出包括前缀和后缀,因此输出将是:
aaa "word2" --sdsdd sdsd {
fffsd
ssss
}
尝试了以下代码:
f = open('/tmp/testt2')
for res in re.findall('aaa "word2" (.*?)}', f.read(), re.S):
print res
只有一些比赛没有开头和结尾的卷曲。
请注意:
感谢您的帮助,
答案 0 :(得分:0)
你可以这样做:
^(?=.*"word2") # ^ - start of the line with pos. lookahead
[^{]+ # anything not a {
{[^}]+} # followed by {, anything in between and a closing }