fork download
  1. import re
  2. s = "1. First sentence. \n2. Second sentence. \n1. Another sentence. \n3. Third sentence."
  3. l = re.findall(r'(?:^|\n)(([0-9]+)\.[\s\S]*?)(?=\n[0-9]+\.|\Z)', s)
  4. curr_num = 0
  5. result = []
  6. for s,num in l:
  7. if curr_num > int(num):
  8. if not result:
  9. result = ['']
  10. result[-1] += s
  11. else:
  12. result.append(s)
  13. curr_num = int(num)
  14.  
  15. print(result)
Success #stdin #stdout 0.02s 9712KB
stdin
Standard input is empty
stdout
['1. First sentence. ', '2. Second sentence. 1. Another sentence. ', '3. Third sentence.']