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 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.
it should replace to 5 + 10/ float(5) - 1
my bad code :

var exp1 = "5 + 4/3 * √5"
var regex = RegEx.new()
regex.compile(" (i dont know) ")
var result = regex.search(exp1)
var final = exp1
if result != null:
    var rep = "float(%s)" % [result.get_string(1)]
    final = exp1.replace(result.get_string(0), rep)
print(final)
Godot version 3.2.4
in Engine by (41 points)

1 Answer

+2 votes
Best answer
var exp1 = "5 + 4/3 * √5"
var regex = RegEx.new()
regex.compile("\\/\\s*(\\d+)")
print(regex.sub(exp1, "/ float($1)"))

The pattern matches a forward slash followed by any number of whitespaces (including zero), followed by at least one digit. This match is then replaced with the string "/ float($1)", where $1 represents the content of the first subgroup (= the digits).

by (1,762 points)
selected by
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.