import re text = open('/root/troupe-standup/WORK-QUEUE.md').read() parts = re.split(r'\n(?=#{2,3} )', text) hdr_re = re.compile(r'^(#{2,3})\s+(Q\d+[^\n]*)') for p in parts: m = hdr_re.match(p) if not m: continue header = p.split('\n',1)[0].strip() sm = re.search(r'^\s*\*\*Status:\s*(.*?)\*\*\s*$', p, re.M) status = sm.group(1) if sm else 'NO-STATUS' status = ' '.join(status.split()) print('## ' + header) print(' STATUS: ' + status) print()