What is an efficient way to pad punctuation with whitespace?
input:
s = \'bla. bla? bla.bla! bla...\'
desired output:
This will add exactly one space if one is not present, and will not ruin existing spaces or other white-space characters:
s = re.sub('(?
This works by finding a zero-width position between a punctuation and a non-space, and adding a space there.
Note that is does add a space on the beginning or end of the string, but it can be easily done by changing the look-arounds to (?<=[^ ])
and (?=[^ ])
.
See in in action: http://ideone.com/BRx7w