Please tell me how to use the re from the next line to get the dictionary:

'ООО "П.Б. Ромашка" (ИНН: 00000000, ОГРН: 9999999)' 

I would like to receive

 {'Наименование': 'ООО "П.Б. Ромашка"', 'ИНН': '00000000', 'ОГРН':'9999999'} 

    2 answers 2

    If specifically from this, then, for example, like this:

     s = 'ООО "П.Б. Ромашка" (ИНН: 00000000, ОГРН: 9999999)' r = re.match("(.*?)\s+\(.*:\s+(.*),.*:\s+(.*)\)", s) d = {'Наименование': r.group(1), 'ИНН': r.group(2), 'ОГРН': r.group(3)} 
    • Thank! This is what you need. - ss_beer
     In [88]: import re In [89]: pat = r'([^\(]*)\s+\(([^:\,\s]*):[\s]*(\d+)[\,\s]+([^:]*)[:\s]+([^\)]*)\)*' In [90]: m = re.search(pat, s, flags=re.U) In [91]: print(m.groups()) ('ООО "П.Б. Ромашка" ', 'ИНН', '00000000', 'ОГРН', '9999999') 

    further task is trivial - to create a dictionary of the elements of a tuple ...

    Where:

     In [92]: s Out[92]: 'ООО "П.Б. Ромашка" (ИНН: 00000000, ОГРН: 9999999)'