Text formatting such as allowing ints only?

im working on something to allow digits only or …
i did this stuff on python and c++ im wondering how i can do in lua?
like this examples:

if string contains 1 to 9 or 0 to 9 or if not 0 to 9 something like this

[1-9] --if string contains numbers from 1 to 9 then return "a number"
[0-9] --if string contains numbers from 0 to 9 then return "a number"
[0-9]+ --if string contains numbers from 0 to 9 or 123 or 12345 then return "a number"
^[1-9] --if string dosent contains numbers from 1 to 9 then return "a number"

if string starts and end with "

[^'"']['"'$] --- if string starts with a " and ends with a " then return "a string value"

and more like not or end containing 5 r or unlimited r’s

thanks

I think what you’re looking for is string patterns.

1 Like

if string.match(input, "^[0-9]+$") then

1 Like

thanks :smiley: also thank to you @Semistare for the docs

also how do i detect if its number because it just give me string not true or false

If it doesn’t find a match, it returns nil. false and nil are both falsy, everything else is truthy, so it’ll equate to true if it returns a string in an if statement.

and how do i detect new lines? and kinda ignore them?

You can use string.gsub to find and replace characters. For a new line, you’d use the newline character \n to find new lines and replace with an empty string.

1 Like

thank you so much :smiley: this topic learned me alot of things

1 Like

and how do i ignore a character like i dont want any NEWLINE be in my text

You can disable multiline for TextBoxes from the properties window. To strip characters from a string, use string.gsub, so to replace newline characters, use:

local singleLine = string.gsub(input, "\n", "")

ok i’ll test it right now but is there a way i can hide it or dont allow it to be in text?

That’s what his code does. It removes all newlines.
Also, if you want to remove everything that isn’t a number automatically, you can do this.
input = input:gsub("[^0-9]", "")

2 Likes