How to split up a string on multiple delimiters but only capture some?
<p>I want to split a string on any combination of delimiters I provide. For example, if the string is:  </p>

<pre><code>s = 'This, I think,., Ú©Ø¨Ø§Ø¨ MAKES , some sense '
</code></pre>

<p>And the delimiters are <code>\.</code>, <code>,</code>, and <code>\s</code>. However I want to capture all delimiters except whitespace <code>\s</code>. The output should be:  </p>

<pre><code>['This', ',', 'I', 'think', ',.,', 'Ú©Ø¨Ø§Ø¨', 'MAKES', ',', 'some', 'sense']
</code></pre>

<p>My solution so far is is using the <code>re</code> module:  </p>

<pre><code>pattern = '([\.,\s]+)'  
re.split(pattern, s)
</code></pre>

<p>However, this captures whitespace as well. I have tried using other patterns like <code>[(\.)(,)\s]+</code> but they don't work.</p>

<p><em>Edit</em>: @PadraicCunningham made an astute observation. For delimiters like <code>Some text ,. , some more text</code>, I'd only want to remove leading and trailing whitespace from <code>,. ,</code> and not whitespace within.</p>

<p><strong>NOTE:</strong> According to the new edit on the question, I've improved my old regex. The new one is quite long but trust me, it's work!</p>

<p>I suggest a pattern below as a delimiter of the function <code>re.split()</code>:</p>

<pre><code>(?&lt;![,\.\ ])(?=[,\.]+)|(?&lt;=[,\.])(?![,\.\ ])|(?&lt;=[,\.])\ +(?![,\.\ ])|(?&lt;![,\.\ ])\ +(?=[,\.][,\.\ ]+)|(?&lt;![,\.\ ])\ +(?![,\.\ ])
</code></pre>

<p>My workaround here doesn't require any pre/post space modification. The thing that make regex work is about how you order the regex expressions with <code>or</code>. My cursory strategy is any patterns that dealing with a space-leading will be evaluated last. </p>

<p>See <a href="https://regex101.com/r/eW8rZ6/3" rel="nofollow">DEMO</a> </p>

<p><strong>Additional</strong></p>

<p>According to @revo's comment he provided an another shorten version of mine which is</p>

<pre><code>\s+(?=[^.,\s])|\b(?:\s+|(?=[,.]))|(?&lt;=[,.])\b
</code></pre>

<p>See <a href="https://regex101.com/r/eW8rZ6/5" rel="nofollow">DEMO</a></p>

python regex