fork(1) download
  1. import re
  2.  
  3. data = """ *** Section with no sub section
  4. *** Section with sub section ***
  5. *** Sub Section ***
  6. *** Another section"""
  7.  
  8. pattern = r'^[ ]{0,2}(?:[*]{3}[ ]?(?P<Section>.*?)|[ ]+[*]{3}[ ]?(?P<SubSection>.*?))(?:[ ]?[*]{3})?$'
  9. regex = re.compile(pattern, re.M)
  10.  
  11. for match in regex.finditer(data):
  12. print(match.groupdict())
  13.  
  14. ''' OUTPUT:
  15. {'Section': 'Section with no sub section', 'SubSection': None}
  16. {'Section': 'Section with sub section', 'SubSection': None}
  17. {'Section': None, 'SubSection': 'Sub Section'}
  18. {'Section': 'Another section', 'SubSection': None}
  19. '''
Success #stdin #stdout 0.02s 9936KB
stdin
Standard input is empty
stdout
{'SubSection': None, 'Section': 'Section with no sub section'}
{'SubSection': None, 'Section': 'Section with sub section'}
{'SubSection': 'Sub Section', 'Section': None}
{'SubSection': None, 'Section': 'Another section'}