Character input restriction for specific characters

I am making a fully working add and subtract program as a nice little project. I would like to know if there is a way to restrict the input of certain characters (e.g. 1 and 0 for binary inputs and A and B for input or subtract inputs). I could always replace all characters that are not these empty lines in order to get rid of them, but doing something like that is pretty tedious.

+4
source share
1 answer

Here is some simple code to filter specified characters from user input:

local filter = "10abAB"
local input = io.read()
input = input:gsub("[^" .. filter .. "]", "")

filter ​​ , . , c, c: local filter = "10abcABC".

, io.read(), , , io.read() , .

- , . string:gsub, , :

input = string.gsub(input, "[^" .. filter .. "]", "").

, , .

gsub [^10abAB], , , , - ^ , , .

, , , :

local input = io.read():gsub("[^10abAB]", "")
+1

All Articles