文件内容如下:
#encoding=utf8
__author__ = "naci"
__title__ = "test script"
__desc__ = "test description"
or __desc__ = """
test description.
"""
# start your script here
问题: 什么是获得作者,头衔和desc的最佳正则表达式? ""也许''或""""""也许''''''
答案 0 :(得分:1)
考虑使用re.findall()函数:
import re
s = '''
#encoding=utf8
__author__ = "naci"
__title__ = "test script"
__desc__ = "test description"
or __desc__ = """
test description.
"""
'''
data = re.findall(r'__(?P<attr>\w+)_ = (?P<val>"[^"]+"|"""[^"]+""")', s)
print(data)
输出(对:键/值):
[('author_', '"naci"'), ('title_', '"test script"'), ('desc_', '"test description"'), ('desc_', '"""\n test description.\n"""')]