Change the values of string in parenthesis which starts with Varchar
<p>I have a python variable (type unicode), which holds a query statement:</p>

<pre><code>CREATE TABLE afg_temp (tadm1_code INTEGER NOT NULL, tadm1_name VARCHAR(10) NOT NULL, test VARCHAR(4));
</code></pre>

<p>I need to replace all the values in each VARCHAR occurence with 254.
Initially I thought it would be enough to use the replace() function but this won't work as the numbers in the parenthesis are random.</p>

<p>Is there a way to do this using regex?</p>

<pre><code>import re

s='''CREATE TABLE afg_temp (tadm1_code INTEGER NOT NULL, tadm1_name VARCHAR(10) NOT NULL, test VARCHAR(4));'''
re.sub('VARCHAR\([0-9]{1,3}\)','VARCHAR(254)',s)
</code></pre>

<p>There are plenty of other solutions and you don't even need python for this. The easiest would be to use sed.</p>

python regex