I have been developing a graphing calculator, and to allow the user's natural input, I have written a function that converts its input into a readable form of python.

This is the first time I use regular expressions, and I think I managed to get each of the expressions to do what I wanted.

However, as can be seen, there are several different cases to consider, so the function is relatively long considering what it does.

I was wondering if there was a better solution than this to solve my problem, but if not, can I improve my implementation?

```
def edit_function_string (func):
"" "Convert the input function to an executable form of Python" ""
func = re.sub (r & # 39; s + & # 39 ;, & # 39; & # 39 ;, func) # Remove blank spaces
func = re.sub (r & # 39; ( d +) x & # 39 ;, r & # 39; 1 * x & # 39 ;, func) # replaces & # 39; nx & # 39; by & # 39; n * x & # 39;
func = re.sub (r & # 39; ^ & # 39 ;, r & # 39; ** & # 39 ;, func) # replaces & # 39; ^ & # 39; by & # 39; ** & # 39;
func = re.sub (r & # 39; (math .)? ceil (ing)? & # 39 ;, math.ceil & # 39 ;, func) # replaces & # 39; ceil (ing) & # 39; with & # 39; math.ceil & # 39;
func = re.sub (r & # 39; (math .)? floor & # 39 ;, & # 39; math.floor & # 39 ;, func) # replaces & # 39; floor & # 39; by & math.floor & # 39;
func = re.sub (r & # 39; (math .f)? abs (olute)? | modulus & # 39 ;, & # 39; math.fabs & # 39 ;, func) # replaces & # 39; abs ( olute) & # 39; with & # 39; math.fabs & # 39;
func = re.sub (r & # 39; (math .)? sqrt | root & # 39 ;, & # 39; math.sqrt & # 39 ;, func) # replaces & # 39; sqrt & # 39; or & # 39; root & # 39; with & math.sqrt & # 39;
func = re.sub (r & # 39; (math .)? log | ln & # 39 ;, & # 39; math.log & # 39 ;, func) # replaces & # 39; log & # 39; log & # 39; or & # 39; ln & # 39; with & math.log & # 39;
func = re.sub (r & # 39; (math .)? exp & # 39 ;, & # 39; math.exp & # 39 ;, func) # replace & # 39; exp & # 39; exp & # 39; by & math.exp & # 39;
func = re.sub (r & # 39; | (. +?) | & # 39 ;, r & # 39; math.fabs ( 1) & # 39 ;, func) # replaces & # 39; | x | & # 39; with & math.fabs (x) & # 39; math.fabs (x) & # 39;
func = re.sub (r & # 39; ([w]+)! | ((. +?) )! & # 39 ;, r & # 39; math.factorial ( 1 2) & # 39 ;, func) # replaces & x 39; x! & # 39; with & math.factorial (x) & # 39;
for f in (& # 39 ;, & # 39 ;, & # 39 ;, & # 39 ;, so & # 39 ;, & # 39; sinh & # 39 ;, & # 39; cosh & # 39 ;, & # 39; tanh & # 39;):
# replaces trigonometric or hyperbolic functions with the correct syntax
func = re.sub (r & # 39; ^ ( d *) {f} (| ([+-*/()]) ( d *) {f} (& # 39;. format (f = f),
r & # 39; 1 2 3math. {f} (& # 39;. format (f = f), func)
# replace trigonometric or inverse hyperbolic functions with the correct syntax
func = re.sub (r & # 39; ^ ( d *) a (rc?)? {f} (| ([+-*/()]) ( d *) a (rc?)? {f} (& # 39;. format (f = f),
r & # 39; 3 1 4math.a {f} (& # 39;. format (f = f), func)
for f, reciprocal in ((& # 39; & # 39; sec & # 39;, & # 39; cos & # 39;), (& # 39; cosec & # 39;, & # 39; without & # 39;), (& # 39; csc & # 39 ;, & # 39;) & # 39;), (& # 39; cot & # 39 ;, & # 39; tan & # 39;),
(& # 39 ;, & & # 39 ;, & # 39; cosh & # 39;), (& cose # & # 39 ;, & # 39; sinh & # 39;), (& # 39; csch & # 39 ;, & # 39; sinh & # 39;), (& # 39; coth & # 39 ;, & # 39; tanh & # 39;)):
# replaces reciprocal trigonometric or hyperbolic functions with the correct syntax
func = re.sub (r & # 39; ^ ( d *) {f} ((. +?) ) | ([+-*/()]) ( d *) {f} ((. +?) ) & # 39 ;. format (f = f),
r & # 39; 3 1 4 (1 / math. {reciprocal} ( 2 5)) & # 39 ;. format (reciprocal = reciprocal), func)
# replaces reciprocal reciprocal trigonometric or hyperbolic functions with the correct syntax
func = re.sub (r & # 39; ^ ( d *) a (rc?)? {f} ((. +?) ) | ([+-*/()]) ( d *) a (rc?)? {f} ((. +?) ) & # 39 ;. format (f = f),
r & # 39; 4 1 5 (1 / math.a {reciprocal} ( 3 7)) & # 39 ;. format (reciprocal = reciprocal), func)
for i in the range (2): # Run twice to deal with overlapping matches
for constant in (& # 39 ;, & # 39; pi & # 39 ;, & # 39; tau & # 39;):
# replaces & # 39; e & # 39 ;, & # 39; pi & # 39; or & # 39; tau & # 39; with & math.e & # 39; math.pi & # 39; math.pi & # 39; or & # 39; math.tau & # 39; Respectfully
# unless it's in another function like: & # 39; math.ceil & # 39;
func = re.sub (r & # 39; ^ ( d *) {constant} (x?) $ | ^ ( d *) {constant} (x?) ([+-*/(]) | & # 39;
r & # 39; ([+-*/()]) ( d *) {constant} (x?) ([+-*/()]) | & # 39;
r & # 39; ([+-*/)]) ( d *) {constant} (x?) $ & # 39 ;. format (constant = constant),
r & # 39; 6 10 1 3 7 11math. {constant} 2 4 8 12 5 9 & # 39; .format (constant = constant), func)
# replaces & # 39; math.ex & # 39 ;, & # 39; math.pix & # 39; or & # 39; math.tau & # 39; with & math.e * x & # 39 ;, math.pi * x & # 39; or & # 39; math.tau * x & # 39; Respectfully
# unless it is part of another function
func = re.sub (r & # 39; math . {constant} x ([+-*/()]) | math.ex $ & # 39 ;. format (constant = constant),
r & # 39; math {constant} * x 1 & # 39; .format (constant = constant), func)
func = re.sub (r & # 39; ([dx]) math . & # 39 ;, r & # 39; 1 2 * math. & # 39 ;, func) # replaces & # 39; nmath & # 39; with & # 39; n * math & # 39;
func = re.sub (r & # 39; ([dx]) (& # 39 ;, r & # 39; 1 * (& # 39 ;, func) # replaces & # 39; n (& # 39; with & # 39; n * (& # 39;
return function
```