As the title says: What is the best way to convert a string from any case to lowercase, keeping the part intact? for example, a line like: FormatDate(%M)==2or stArTDate(%Y/%m)==11/3, and I want to convert it to FormatDate(%M)==2or stArTDate(%Y/%m)==11/3, that is, change it to lowercase, except for the part between the curly braces (). For the first example, I approached something like this:
>>> import re
>>> fdt = re.compile('(F|f)(O|o)(R|r)(M|m)(A|a)(T|t)(D|d)(A|a)(T|t)(E|e)\(')
>>> ss = "forMatDate(%M)==2"
>>> if fdt.match(ss):
... SS = ss.split('(')
... SS[0] = SS[0].lower()
... ss = "(".join(SS)
...
>>> print ss
formatdate(%M)==2
While it works perfectly, I didn’t really like to do this. A regular expression is ugly, and it practically limits a particular line. Is there a better (hence dynamic) way to do this? Thanks in advance. Hooray!!
Update:, , : formatdate(), startdate() enddate() UserName==JohnDee ... ( ), , - . , Krumelur's script.
>>> fdt = re.compile('\(%[dmwyMW].*\)')
>>> ss = "formatDate(%M)==4"
>>> st = "UserName==JohnDee"
>>>
>>> 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
...
>>> print dt_lower(ss)
formatdate(%M)==4
>>>
>>> print dt_lower(st)
username==johndee
, . . !!