我有一个看起来像这样的文本文件:
This is a text file
game:
"
a
b
"
symbol : "o"
我想在“ game:”旁边加上引号之间的行。 我尝试使用re,例如:
contents = file.read()
re.findall((?<=game)"([^"]*)",contents)
但这不起作用... 有谁可以帮助我吗? 谢谢!
答案 0 :(得分:1)
您需要考虑game: "
之间和之后的换行符和空格:
re.findall(r"game: \n\"\n([^\"]*)",contents)
答案 1 :(得分:0)
尝试使用此模式。让我知道您是否需要进行任何调整(?<=game: \n").*?(?=")
答案 2 :(得分:0)
您应该将正则表达式用单引号引起来,因为您的模式会查找双引号:
import re
s = '''This is a text file
game:
"
a
b
"
symbol : "o"'''
print(re.findall(r'game:\s*\n"\n([^"]*)"', s))
这将输出:
['a\nb\n']