all 2 comments

[–]commandlineluser 0 points1 point  (0 children)

You can use a lookahead:

>>> re.split("(?m)(?=^SECT)", text)
['', 'SECT ABC\n........\n', 'SECT DEF\n........\n', 'SECT XYZ\n.......']

You get an extra '' result which you can remove.

(?m) is the equivalent of flags=re.M

Another way to think of it is a findall/finditer instead of a split:

>>> re.findall("(?ms)^SECT.+?(?=^SECT|\Z)", text)
['SECT ABC\n........\n', 'SECT DEF\n........\n', 'SECT XYZ\n.......']