This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

How to replace text in expression?

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


(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..

in Engine by (41 points)

1 Answer

+1 vote
Best answer

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 =
    var result =
    var rep = 'pow(' + result.get_string(1) + ',' + result.get_string(2) + ')'
    var new = exp1.replace(result.get_string(0), rep)
by (22,704 points)
selected by

wow, thanks for help☺

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.

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 =
    var result =
    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)

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

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...

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.