How to replace text, like this ( 2 + 3^5) to (2 + pow(3, 5)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Konrad

How to replace text in expression?

(2 + 5*2 - 13^4 + 7)

to

(2 +5*2 - pow(13,4) + 7)

i made an expression, but user dont understand that pow = ^.
i have no ideas how to solve this problem…

:bust_in_silhouette: Reply From: jgodfrey

Here’s a (relatively naive) example of using a regex (as I suggested in the precursor to this question)…

func _ready():
	var exp1 = "5 + 17.32^3.56 * √5"
	var regex = RegEx.new()
	regex.compile("(\\d+\\.*\\d*)\\^(\\d+\\.*\\d*)")
	var result = regex.search(exp1)
	var rep = 'pow(' + result.get_string(1) + ',' + result.get_string(2) + ')'
	var new = exp1.replace(result.get_string(0), rep)
	print(new) 

wow, thanks for help☺

Konrad | 2020-12-21 18:20

I really don’t know how to thank you, before that I tried to do what you told me, but there were no examples anywhere. I tweaked your code for myself and it worked !!! I don’t even know what I would do without your help. I’ve been trying to solve this problem for about 5 days.

Konrad | 2020-12-21 19:02

Here’s a somewhat improved version of the earlier code. At least now, it won’t explode if the regex doesn’t find what it’s looking for…

func _ready():
	var exp1 = "5 + 17.32^3.56 * √5"
	var regex = RegEx.new()
	regex.compile("(\\d+\\.*\\d*)\\^(\\d+\\.*\\d*)")
	var result = regex.search(exp1)
	var final = exp1
	if result != null && result.strings.size() == 3:
		var rep = "pow(%s,%s)" % [result.get_string(1), result.get_string(2)]
		final = exp1.replace(result.get_string(0), rep)
	print(final)

jgodfrey | 2020-12-21 19:24

Hi i am facing the problem that expression does not count 1/2 but counts 1 / 2.0 . Can I do something so that expression counts 1/2, etc. Thank you for help

Konrad | 2021-01-21 20:52

I assume that when you say does not count 1/2 you actually mean that it results in 0. That’s just standard integer math. When you divide an integer by another integer, the result is also an integer. So, it will result in 0 (because the real answer of 0.5 is not an integer). That’s typical of most programming languages.

To get a floating point result, at least one of the numbers must, itself, be a float - as you’ve discovered.

So, either 1 / 2.0 or 1.0 / 2 will result in the expected answer of 0.5. So, one or both numbers need to be promoted to a float to get a float answer. You can do that by adding a .0 to at least one of the values, or wrapping it in a float call like float(1)/2 or 1/float(2).

You’ll need to do one of those to get a float answer…

jgodfrey | 2021-01-21 22:35

Hello, I need your help again. How do I find the text after the sign "/". For example in the expression: 5 + 10 / 5 - 1 i need to find "5" and replace with float(5) or 5.0. I tried to find the numbers that stand after / ,but it didn’t work out for me. Thanks for the help.

Konrad | 2021-03-27 15:35