I am expecting an input to look like: "x^3" and would like to replace this with a string of "pow(x, 3)". However the whole input may look like "x^3 + x^2 + 4". Here I want each instance of the "^" operator to signify a conversion to the "pow()" substring instead.
This means that the substring I am replacing it with is influenced by what it finds, but I need wildcard operators either side of the "^" if I am to do it with regular expressions.
Is there a way to "store" what the wildcard happens to be at a given instance to reinsert it in the replacement string?
i.e. in x^3, the 3 gets stored and put back into pow(x, 3)
Cases:
"x^2" -> "pow(x, 2)"
"x^3 + x^2" -> "pow(x,3) + pow(x, 2)"
"x^4 + y^19" -> "pow(x, 4) + pow(y, 19)"