2017-02-21 57 views
-3

文件内容低于:什么是多文本最好的正则表达式?

#encoding=utf8 
__author__ = "naci" 
__title__ = "test script" 
__desc__ = "test description" 
or __desc__ = """ 
    test description. 
""" 
# start your script here 

问题: 什么是为获取作者,标题,和desc最好的正则表达式?的 “” 也许 '' 或 “” “” “” 也许 '' '' ''

+1

[学习正则表达式]的可能重复(http://stackoverflow.com/questions/4736/learning-regular-expressions) – Sayse

+0

你有没有尝试过任何东西?除了研究工作之外,您还应该为我们提供预期产出。您可以访问[问]是否需要帮助。 – Niitaku

回答

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"""')]