<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
kj wrote:
<blockquote cite="mid:h64h3o$f3c$1@reader1.panix.com" type="cite">
<pre wrap="">
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.:
</pre>
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">import re
re.split('^', 'spam\nham\neggs\n')
</pre>
</blockquote>
</blockquote>
</blockquote>
<pre wrap=""><!---->['spam\nham\neggs\n']
</pre>
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">re.split('(?m)^', 'spam\nham\neggs\n')
</pre>
</blockquote>
</blockquote>
</blockquote>
<pre wrap=""><!---->['spam\nham\neggs\n']
</pre>
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">bol_re = re.compile('^', re.M)
bol_re.split('spam\nham\neggs\n')
</pre>
</blockquote>
</blockquote>
</blockquote>
<pre wrap=""><!---->['spam\nham\neggs\n']
Am I doing something wrong?
</pre>
</blockquote>
Just split on the EOL character: the "\n":<br>
re.split('\n', 'spam\nham\neggs\n')<br>
['spam', 'ham', 'eggs', '']<br>
<br>
The "^" and "$" characters do not match END-OF-LINE, but rather the
END-OF-STRING, which was doing you no good. <br>
<br>
<br>
Gary Herron<br>
<br>
<br>
<br>
<br>
<blockquote cite="mid:h64h3o$f3c$1@reader1.panix.com" type="cite">
<pre wrap="">
kynn
</pre>
</blockquote>
<br>
</body>
</html>