2017-02-21 4 views
-3

ファイルの内容は以下の通りです:複数のテキストに最適な正規表現は何ですか?

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

質問: GET著者、タイトル、およびDESCのための最高の正規表現は何ですか? "" 多分 '' または "" "" "" 多分 '' '' '

+1

[正規表現の学習](http://stackoverflow.com/questions/4736/learning-regular-expressions)の可能な複製 – Sayse

+0

試しましたか?また、あなたの研究努力に加えて、期待される成果を提供してください。助けが必要な場合は[ask]にアクセスできます。 – 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"""')] 
関連する問題