fork download
  1. import re
  2.  
  3. regex = r"^(\d{1,3}\.?(?:\d{1,3}\.?)*)? ?([^\W\d]+)"
  4.  
  5. s = ("section\n"
  6. "1 section\n"
  7. "2. section\n"
  8. "2.1 subsection\n"
  9. "2.1.1 subsection\n"
  10. "2.1.1. subsection")
  11.  
  12. print(re.findall(regex, s, re.MULTILINE))
Success #stdin #stdout 0.03s 9560KB
stdin
Standard input is empty
stdout
[('', 'section'), ('1', 'section'), ('2.', 'section'), ('2.1', 'subsection'), ('2.1.1', 'subsection'), ('2.1.1.', 'subsection')]