How to replace the pattern w{3,3} with w{3}
<p>I meant any numbers in this regex expression.</p>

<pre><code>w{1,1} --&gt; w{1}
w{2,2} --&gt; w{2}
</code></pre>

<p>and so on.</p>

<p>Find <code>w\{(\d*),\1\}</code> and replace it with <code>w{\1}</code>.</p>

<p>Here is a full example with python code:</p>

<pre><code>import re
 re.sub(r'w\{\s*([0-9]+)\s*,\s*\1\s*\}', r'w{\1}', 'w{1,1}')
</code></pre>

<p>Explanation:</p>

<ul>
<li>we have to escape the curly braces: <code>\{</code> and <code>\}</code></li>
<li>we need one ore more number: <code>[0-9]+</code></li>
<li>this number is surrounded with zero or more whitespaces: <code>\s*</code></li>
<li>then the same numbers again, with a <em>backreference</em>: <code>\1</code></li>
<li>finally, we can use the backreference in the replacement too: <code>w{\1}</code></li>
</ul>

python regex python-2.7 python-3.x