import re
string = "(cos(0) + sin(180)) * tan(45.0) + not_a_tan(1)"
print re.sub(r'\b(cos|sin|tan)\s*\(\s*(\d+(?:\.\d+)?)\s*\)', r'\1(degres(\2))', string)
import re
from lxml import etree
doc = \
"""
<body>
<img alt='fdsfsd' src='CONST text1 text2'/>
adasd
<img alt='sadsdd' src='CONST text3 text4'/>
adasdas
</body>
"""
tree = etree.fromstring(doc, parser=etree.HTMLParser())
for img in tree.xpath('//img[@src]'):
img.attrib['src'] = re.sub(r'\s+', '_', img.attrib['src'])