This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Prevent textwrap from breaking words at hyphens
Type: enhancement Stage:
Components: None Versions:
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: gward Nosy List: fourmanoit, gpolo, gward, kraai, rhettinger
Priority: normal Keywords:

Created on 2007-04-18 06:40 by kraai, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg55065 - (view) Author: Matt Kraai (kraai) Date: 2007-04-18 06:40
I'm using textwrap to wrap text that contains URLs that contain hyphens.  When it wraps these URLs after a hyphen, it breaks the URL.  I wish there was a way to prevent it from doing so.
msg55066 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-04-19 06:37
FWIW, one workaround is to "monkey patch" the module:

  textwrap.TextWrapper.wordsep_re = re.compile(r'(\s+|(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))')

Another workaround is to "hide" the hyphens during wrapping and then restore them.
  def myfill(text, width=70, **kwargs):
     althyphen = chr(127)
     text = text.replace('-', althyphen)
     result = wrap(text, width, **kwargs)
     return result.replace(althyphen, '-')

That being said, I'm +1 on adding a keyword argument treating hyphens as non-breaking.
msg67673 - (view) Author: Sylvain Fourmanoit (fourmanoit) Date: 2008-06-03 19:21
> That being said, I'm +1 on adding a keyword argument treating hyphens
> as non-breaking

It's now in Python 2.6:

This issue should probably be closed.
msg67674 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2008-06-03 19:30
"Duplicate" of issue2659
Date User Action Args
2022-04-11 14:56:23adminsetgithub: 44859
2008-06-03 19:30:52gpolosetstatus: open -> closed
nosy: + gpolo
resolution: duplicate
messages: + msg67674
2008-06-03 19:21:56fourmanoitsetnosy: + fourmanoit
messages: + msg67673
2007-04-18 06:40:13kraaicreate