0 votes

Hi everyone,

I have a label I can enter numbers into and I can detect if single numbers are in the text with (for each number respectively):

var oneIsWritten = false

var one = bbcode_text.find("1")
    if one >= 1:
        oneIsWritten = true
        print ("There's a 1 in the text!")

(I can go the same way for "there's no 1 in the text" with "if one <= 0: ...)

Now, if I'd like to know if there is actually any number between 1 and 9 written (explicitly leaving the 0 out), doing this with all the numbers true/false on their own would be too much handywork. I'm sure there is an elegant way of achieving this (maybe with an array...?)

What could a code for this look like?

Godot version 3.2.3
in Engine by (313 points)

2 Answers

0 votes
Best answer

https://docs.godotengine.org/en/stable/classes/class_regex.html

You can use \d to match a digit and [1-9] to not match 0

by (6,280 points)
selected by

I guess yes, that's it, thank you! ...so many things to learn ; )

0 votes

Or You can use string operands :

for x in range(1,9) :
     iff bbcode_text.find(str(x)) :
           print ( "There is " + str(x) + " in the text" )
by (1,110 points)

Thank you, so for 1 to 9 I'd need for x in range(1,10): then, I see.
It appears that in this case I always get nine line printed, one for each number, no matter which number I enter...

For getting the info if there is actually any number written or not, NatureSilver5401 on reddit suggests:

var writtennumbers = Array()
writtennumbers.append("whatever you like")
for number in 9:
    if str(number+1) in text:
        print("There is a " + str(number+1) + " in the text"
        writtennumbers.append(true)
    else:
        writtennumbers.append(false)
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 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 webmaster@godotengine.org with your username.