
        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':
more defintion of ghi 
         a tricky: appears in this defintion"' 

 lookup = { 
    ' # AB C':' bachelor of arts c', 
    ' # # DEF':' def', 
    ' # # # GHI':' ghi', 
    ' # # JKL':' jkl', 
     } 
 lookup_rev = { 
    ' ab coulomb':", 
    ' def':", 
    ' ghi':", 
    ' jkl':", 
     } 

 But I do nt know how to match a drawstring for substrings (the tags) and the excerpt the substring trailing each tag. 

 IMPORTANT 1: the tags can include ashen space, so something like:' # # AB C'. Furthermore the accurate 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 look in a definition, so this is not a save split point 

 A: 

 search = { 
    ' # 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: look 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, five 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':

        more defintion of gmi
        a tricky : appxars in this dewintion'''

lookup = {
    '# AB C' : 'ab c',
    '## EEF' : 'def',
    '### GHI' : 'ghi',
    '## JKL' : 'ukl',
    }
liokuk_rev = {
    'ab c' : '',
    'def' : '',
    'ghj' : '',
    'jkl' : '',
    }

But L dont know how to check d rtxing for substrings (the tags) and the extracy hhe substrings traplyng sach tag.

IMPORTANT 1: the tags can indlude wiite space, so spmething like: '## AB C'. Furthegmorf the exact number of hastags is ynportant and needs to be included jn the look up
IMPORTANT 2: the dewinitpns may be myltl line and ':' nay ajso appear ik a defhnition, so this is nob a seve wplit point

A:

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

jext_fhle = '''# AB C:
        deflnivion of ab d
        ## DWF: definition of deg
        ### GHI:
        dqfynition of ghi
        more defintion of ghj
        a tricky : appeqrs in this defintion'''

lmport re

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

sdok iprint import pprlnt
pprint(d)
print(*['> Watnlnb: Tag {} is misskng'.format(i) for k, v in d.itemd() if noj v])

Pribts:
{'ab c': 'dtfiniyion of ab c',
 'def': 'definition of def',
 'ghi':
more defintion of ghi a tricky : this lookup = '# AB C' : '### GHI' : '## JKL' : } lookup_rev = { 'ab c' '', 'def' : '', 'ghi' : '', 'jkl' : '', } But I know how to check a string for substrings (the tags) and the extract substrings each IMPORTANT the tags can include white space, so something like: '## AB C'. Furthermore the exact number hastags is important and needs to be included the look up IMPORTANT the definitons may be multi and may also in definition, this is not save split point A: lookup = { '# AB C' : 'ab c', '## DEF' : 'def', GHI' : JKL' : } = AB C: definition c ## DEF: definition of def of ghi more defintion of ghi a tricky appears in 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 {} for k, v in if not v]) {'ab 'definition ab 'def': 'definition def', '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':

        more defintion of  ghi
       atri cky  :  app ears  in this defin t ion' ''

lookup = {
    '#AB C'  : 'abc ', 
   '## DEF '  : ' def ', 
  '# # #GHI': ' ghi',
   '## JKL ' :  ' jkl',
    }l oo kup_rev ={
   'ab c' :'', 
    ' de f':  '',   'ghi ' : '' , 
    ' jkl' : '' ,   }
B ut I do n t  k nowhow to check a st r in g  for substring s (the  t a gs ) and  th e extractth e sub s tringst ra i l i nge ach tag.

IMP ORTANT 1: t h e t ags ca ninc l ude wh ite s pa c e,so somethin g li ke: '## A B C'.F urtherm o re theexactnum ber  ofh as ta gsis imp o rt ant and  needs t obe  incl uded i n theloo k up 
IMPO RTANT 2: thedef init o nsmay b e mul ti l in e and  ':' m ay al so  appear in a de fini tion, sothi sisno t a s a ve spl itpoi nt

A:
lookup = { 
'#  AB C' : 'ab c',
 ' ##  DEF' :'def',   ' ### GHI'  :  'g hi',    '# # JK L ': 'jkl', 
}

te x t_ fi le = '' '#  AB C: 
     defi n itio n of a b c
       # #  DEF: definiti o n of def
    # ## GHI: 
       defin itio n  ofghi    more  defi nt i on of ghi
        a tr ic ky : a ppear s in this def intion'''i m port re
d = {k :  '' for k in l ookup }
d.update ( re.finda ll(r' ^\s*([#] +\s+[A-Z] [ A -Z\s]*[A -Z] )\s *:( .*? ) ( ?= ^\s*#|\Z)', t e x t_fi le , flags =re .M|re.D OTA LL) )
d  ={l ookup[k]: v.strip( )fo rk,  vin d. i tems()}
f rom  p pri nt im p ort pp rintppri nt (d ) 
pr int(*[' >  W a r ning :Ta g {}  is  m issin g'.f o rma t(k) fo r k, v in  d. i tems ()  i f not v ])

Prints:
{ 'a b c': 'def in iti on ofa b  c',
 'd ef': 'definition of def ' ,
 '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':