-3

The file content is below:

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

question: what's the best regex for get author, title, and desc? the "" maybe '' or """""" maybe''''''

nanci
  • 411
  • 1
  • 5
  • 17
  • 1
    Possible duplicate of [Learning Regular Expressions](http://stackoverflow.com/questions/4736/learning-regular-expressions) – Sayse Feb 21 '17 at 08:22
  • Have you tried anything? You should also provide us, in addition to your research effort, the expected output. You can visit [ask] if you need help. – Niitaku Feb 21 '17 at 08:34

1 Answers1

1

Consider using re.findall() function:

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)

The output(pairs: key/value):

[('author_', '"naci"'), ('title_', '"test script"'), ('desc_', '"test description"'), ('desc_', '"""\n    test description.\n"""')]
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105