'How to retrieve information in the first section of the raw data only by regular expressions?

Below is a sample of the raw data which my code will process by regular expressions:

raw_data = '''
name        :   John
age         :   26
gender      :   male
occupation  :   teacher

Father
---------------------
name        :   Bill
age         :   52
gender      :   male

Mother
---------------------
name        :   Mary
age         :   48
gender      :   female
'''

I want to retrieve the following part of information from the raw data and store it in a dictionary:

dict(name = 'John', age = 26, gender = 'male', occupation = 'teacher')

However, when I run my code as follows, it does not work as I expect:

import re
p = re.compile('[^-]*?^([^:\-]+?):([^\r\n]*?)$', re.M)
rets = p.findall(raw_data)

infoAboutJohnAsDict = {}

if rets != []:
  for ret in rets:
    infoAboutJohnAsDict[ret[0]] = ret[1]
else:
  print("Not match.")

print(f'rets = {rets}')
print(f'infoAboutJohnAsDict = {infoAboutJohnAsDict}')

Can anyone give me any suggestion about how I should modify my code to achieve what I intend to do?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source