Q:

Python: Parse String for Tags to divide it into substrings to fill json files

'Q:
How do i check if a text file contains all tags (for example predifned in a lookup table) and then parse all substrings that appear between these tags and use them to fill a new json file.
Example Text file:
# AB C: 
definition of ab c
## DEF: definition of def
### GHI: 
definition of ghi
more defintion of ghi
a tricky : appears in this defintion

Example tag Look-up table:
# AB C : ab c
## DEF : def
### GHI : ghi
## JKL : jkl

Desired json output:
{
 'ab c': 'definition of ab c',
 'def': 'definition of def',
 'ghi': 'definition of ghi',
 'jkl': ''
}
> Warning: Tag '## JKL' is missing

Approach So Far:
Create Two Tag Dictionaries, one like the example look-up table and the second with values of the first as keys and empty strings as values.
text = '''# AB C: 
        definition of ab c
        ## DEF: definition of def
        ### GHI: 
        definition of ghi
        more defintion of ghi
        a tricky : appears in this defintion'''

lookup = {
    '# AB C' : 'ab c',
    '## DEF' : 'def',
    '### GHI' : 'ghi',
    '## JKL' : 'jkl',
    }
lookup_rev = {
    'ab c' : '',
    'def' : '',
    'ghi' : '',
    'jkl' : '',
    }

But I dont know how to check a string for substrings (the tags) and the extract the substrings trailing each tag.

IMPORTANT 1: the tags can include white space, so something like: '## AB C'. Furthermore the exact number of hastags is important and needs to be included in the look up
IMPORTANT 2: the definitons may be multi line and ':' may also appear in a definition, so this is not a save split point

A:

lookup = {
    '# AB C' : 'ab c',
    '## DEF' : 'def',
    '### GHI' : 'ghi',
    '## JKL' : 'jkl',
}

text_file = '''# AB C:
        definition of ab c
        ## DEF: definition of def
        ### GHI:
        definition of ghi
        more defintion of ghi
        a tricky : appears in this defintion'''

import re

d = {k: '' for k in lookup}
d.update(re.findall(r'^\s*([#]+\s+[A-Z][A-Z\s]*[A-Z])\s*:(.*?)(?=^\s*#|\Z)', text_file, flags=re.M|re.DOTALL))
d = {lookup[k]:v.strip() for k, v in d.items()}

from pprint import pprint
pprint(d)
print(*['> Warning: Tag {} is missing'.format(k) for k, v in d.items() if not v])

Prints:
{'ab c': 'definition of ab c',
 'def': 'definition of def',
 'ghi': 'definition of ghi\n'
        '        more defintion of ghi\n'
        '        a tricky : appears in this defintion',
 'jkl': ''}
> Warning: Tag jkl is missing

