<br><br><div class="gmail_quote">On Fri, Aug 14, 2009 at 2:23 PM, kj <span dir="ltr"><no.email@please.post></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<br>
<br>
Sometimes I want to split a string into lines, preserving the<br>
end-of-line markers.  In Perl this is really easy to do, by splitting<br>
on the beginning-of-line anchor:<br>
<br>
  @lines = split /^/, $string;<br>
<br>
But I can't figure out how to do the same thing with Python.  E.g.:<br>
<br>
>>> import re<br>
>>> re.split('^', 'spam\nham\neggs\n')<br>
['spam\nham\neggs\n']<br>
>>> re.split('(?m)^', 'spam\nham\neggs\n')<br>
['spam\nham\neggs\n']<br>
>>> bol_re = re.compile('^', re.M)<br>
>>> bol_re.split('spam\nham\neggs\n')<br>
['spam\nham\neggs\n']<br>
<br>
Am I doing something wrong?<br>
<br>
kynn<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br><div>You shouldn't use a regular expression for that.</div><div><br></div><div><div>>>> from time import time</div><div>>>> start=time();'spam\nham\neggs\n'.split('\n');print time()-start;</div>

<div>['spam', 'ham', 'eggs', '']</div><div>4.6968460083e-05</div><div>>>> import re</div><div>>>> start=time();re.split(r'\n', 'spam\nham\neggs');print time()-start;</div>

<div>['spam', 'ham', 'eggs']</div><div>0.000284910202026</div><div><br></div></div><div><br></div>