string manipulation: partly convert to lowercase
<p>As the title says: What's best way of converting a string from any-case to lowercase keeping a portion unchanged? e.g. a string like: <code>FormatDate(%M)==2</code> or <code>stArTDate(%Y/%m)==11/3</code> and I want to convert it to <code>formatdate(%M)==2</code> or <code>startdate(%Y/%m)==11/3</code> i.e. change it to lowercase except the part in between the braces <code>()</code>. For the first example, I came up something like this:</p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; fdt = re.compile('(F|f)(O|o)(R|r)(M|m)(A|a)(T|t)(D|d)(A|a)(T|t)(E|e)\(')
&gt;&gt;&gt; ss = "forMatDate(%M)==2"
&gt;&gt;&gt; if fdt.match(ss):
...   SS = ss.split('(')
...   SS[0] = SS[0].lower()
...   ss = "(".join(SS)
... 
&gt;&gt;&gt; print ss
formatdate(%M)==2
</code></pre>

<p>Whilst it works just fine, I didn't quite like it doing this way. The regular expression is ugly and it makes thing pretty much restricted to a particular string. Is there any better (hence, dynamic) way of doing that? Thanks in advance. Cheers!!</p>

<p><hr>
<strong>Update:</strong></p>

<p>I probably didn't say it very clearly: it's not always <code>formatdate()</code>, sometimes it's <code>startdate()</code> or <code>enddate()</code> along with <code>UserName==JohnDee</code> and so on.. (it's part of the user-input) but the format is always the same and I wanted something reusable. So, this is the updated version based on <code>Krumelur's</code> script.</p>

<pre><code>&gt;&gt;&gt; fdt = re.compile('\(%[dmwyMW].*\)')
&gt;&gt;&gt; ss = "formatDate(%M)==4"
&gt;&gt;&gt; st = "UserName==JohnDee"
&gt;&gt;&gt;
&gt;&gt;&gt; def dt_lower(sX):
...   if fdt.search(sX):
...     p1,p2 = sX.split('(',1)
...     sX = "%s(%s" % (p1.lower(), p2)
...   else: sX = sX.lower()
...   return sX
... 
&gt;&gt;&gt; print dt_lower(ss)
formatdate(%M)==4
&gt;&gt;&gt;
&gt;&gt;&gt; print dt_lower(st)
username==johndee
</code></pre>

<p>This is exactly what I wanted. Thanks everyone for helping. Cheers!!</p>
