Splitting on '^' ?

kj no.email at please.post
Fri Aug 14 22:23:20 CEST 2009

Sometimes I want to split a string into lines, preserving the
end-of-line markers.  In Perl this is really easy to do, by splitting
on the beginning-of-line anchor:

  @lines = split /^/, $string;

But I can't figure out how to do the same thing with Python.  E.g.:

>>> import re
>>> re.split('^', 'spam\nham\neggs\n')
>>> re.split('(?m)^', 'spam\nham\neggs\n')
>>> bol_re = re.compile('^', re.M)
>>> bol_re.split('spam\nham\neggs\n')

Am I doing something wrong?


More information about the Python-list mailing list