Regular Expression to replace dot with space before parentheses
<p>I am working on some customer comments that some of them did not follow grammatical rules. For Example <code>(Such as s and b.)</code> in the following text that provides more explanation for previous sentence is surrounded by two dots. </p>

<pre><code>   text = "I was initially scared of ANY drug after my experience. But about a year later I tried. (Such as s and b.). I had a very bad reaction to this."
</code></pre>

<p>First, I want to find <code>. (Such as s and b.).</code> and then  replace the  dot before <code>(Such as s and b.)</code> to space. This is my code, but it does not work. </p>

<pre><code>text = re.sub (r'(\.)(\s+?\(.+\)\s*\.)', r' \2 ', text )
</code></pre>

<p>Output should be: </p>

<pre><code> "I was initially scared of ANY drug after my experience. But about a year later I tried  (Such as s and b.). I had a very bad reaction to this."
</code></pre>

<p>I am using python.</p>

<p>The sample provided does not make much sense because the only change is that the ` character is moved one position to the left.</p>

<p>However, this might do the trick (to keep the dot inside the paranthesis):</p>

<pre><code>text = re.sub(r'\.\s*\)\s*\.', '.)', text)
</code></pre>

<p>Or this to have it outside:</p>

<pre><code>text = re.sub(r'\.\s*\)\s*\.', ').', text)
</code></pre>

<p><strong>Edit:</strong> Or maybe you're looking for this to replace the dot before the opening paranthesis?</p>

<pre><code>text = re.sub(r'\.(?=\s*\(.*?\)\.)', ').', text)
</code></pre>

python regex