我有以下字符串(文件):
s = '''
\newcommand{\commandName1}{This is first command}
\newcommand{\commandName2}{This is second command with {} brackets inside
in multiple lines {} {}
}
\newcommand{\commandName3}{This is third, last command}
'''
现在我想使用Pythonre
包将数据提取到字典中,其中key
是命令名(\commandName1
、\commandName2
和\commandName3
),值是This is first command
、This is second command with {} brackets inside in multiple lines {} {}
和This is third, last command
.我试了一下,比如:
re.findall(r'\\newcommand{(.+)}{(.+)}', s)
个
但它不起作用,因为第二个指挥部里面有{}
个.要做到这一点,最简单的方法是什么?