2017-02-21 58 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"""')]