samedi 25 mars 2017

How to replace part of a string with an added condition

I am looking to replace part of a string with another string but only if a condition is met first. An example looks like this:

string='tan(x)*arctan(x)'

I want to obtain string='np.tan(x)*np.arctan(x)'

How can I use string.replace('tan','np.tan') only if 'tan' is not preceded by 'arc'?

string=string.replace('tan','np.tan')

string=string.replace('arctan','np.arctan')

print string

Thanks for any advice

Aucun commentaire:

Enregistrer un commentaire