Answer the question
In order to leave comments, you need to log in
How to replace a substring in Python while keeping part of that string?
The essence of the question is this, you need in the input line like
string = "cos(90) + cos(170) + sin( 80)" #могут быть и другие математические функции
out_string = "cos(degres(90)) + cos(degres(170)) + sin(degres( 80))"
(cos *\( *(?P<degres>[\d]+) *\))
Answer the question
In order to leave comments, you need to log in
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)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question