title: How to insert space by punctuation?
body: <p>I have strings like so: <code>hey what is up!</code>, <code>&quot;what did you say?&quot;</code>, <code>&quot;he said 'well'&quot;</code>, etc. and a regex expression like so: <code>[!%&amp;'\(\)$#\&quot;\/\\*+,-.:;&lt;=&gt;?@\[\]^_´</code>{|}~]´. These are my delimiters and into the strings shown a space shall be inserted like so: <code>&quot;hey what is up !&quot;</code>, <code>&quot;what did you say ?&quot;</code>, <code>&quot;he said ' well '&quot;</code>. So if one of the delimiters is in front of another character sequence, add a space, and if its is after, add space as well.</p>
  <p>How can I achieve this? I do not want to split by these delimiters.</p>
  <p>Here's my solution but I would be curious how to solve it with regex.</p>
  <pre><code>    space = set(&quot;[!%&amp;'()$#\&quot;/\*+,-.:;&lt;=&gt;?@[]^_´`{|}~]&quot;)
      for sent in self.sentences:
          sent = list(sent)
          for i, char in enumerate(sent):
              # Make sure to respect length of string when indexing
              if i != 0:
                  # insert space in front if char is punctuation
                  if sent[i] in space and sent[i - 1] != &quot; &quot;:
                      sent.insert(i, &quot; &quot;)
              if i != len(sent)-1:
                  # insert space after if char is punctuation
                  if sent[i] in space and sent[i + 1] != &quot; &quot;:
                      sent.insert(i + 1, &quot; &quot;)
  </code></pre>