RE replace problem too

Lie Ryan lie.1296 at gmail.com
Fri Jun 12 09:02:03 EDT 2009


oyster wrote:
> in my case, I want to replace all the function name with '', that is
> sin(1)  ->  (1)
> sin(pi*(2+4))  -> (pi*(2+4))
> how can I use RE in this situation? thanx

this works if there is no implicit multiplication:

re.sub('\w+\(', '(', 'sin(pi*(2+4))')

this one might be more robust:
re.sub('[a-zA-Z]\w*\s*\(', '(', 'sin(pi*cos(2+4))')



More information about the Python-list mailing list