On Feb 28, 4:06 pm, "Ryan K" <[EMAIL PROTECTED]> wrote:
> I'm trying to text wrap a string but not using the textwrap module. I
> have 24x9 "matrix"  and the string needs to be text wrapped according
> to those dimensions. Is there a known algorithm for this? Maybe some
> kind of regular expression? I'm having difficulty programming the
> algorithm.


Try:

import re
sample_text = """Personal firewall software may warn about the
connection IDLE makes to its subprocess using this computer's internal
loopback interface.  This connection is not visible on any external
interface and no data is sent to or received from the Internet."""

# assume 24 is sufficiently wide:

lines = map(lambda x: x.rstrip(),
re.findall(r'.{1,24}(?:(?<=\S)\s|$)', sample_text.replace("\n", " ")))

print "\n".join(lines)


--
Hope this helps,
Steven

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to