Remove all non-numbers from a string

How would I remove all non numbers from a string. For example I have "AB1D45" I should get "145" back.

I think you should use string.gsub() How to use String.gsub?

local Table = Value:split(", ")
		local black = {}
		for i, v in pairs(Table) do
			if not tonumber(v) then
				table.insert(black,i)
			end
		end
		for i, v in pairs(black) do
			table.remove(Table, v)
		end
table.concat(Table)

solution

gsub did not work well for me.

Here is a method

function numbers_only(_string)
	local number_only = ""
	for i = 1, #_string do
		if tonumber(string.sub(_string,i,i)) ~= nil then
			number_only = number_only .. string.sub(_string,i,i)
		end
	end
	return number_only
end
print(numbers_only("AB1D45"))

That is better than my solution, Thank you.

All of the answers on this post I have seen so far are overcomplicated. You can just simply use the gsub function with a proper pattern.

local num = string.gsub("AB1D45", "%D+", "")
print(num) --> 145
1 Like