Getting a table of numbers from a string

examples of what i want:
input: “1 WEEKS 2 DAYS”
output: {1, 2}
input: “1WEEKS2”
output: {1, 2}

if you only have to get numbers less than 10 this should work

local s = "1hello 2"
local result = {}
for _, v in ipairs(s:split("")) do
	if tonumber(v) then
		table.insert(result, tonumber(v))
	end
end

i will need numbers more then 10

actually would replacing all non-letters with spaces and then using string spilt for spaces not return any of the spaces?

You can use string.gmatch to iterate over all matches:

local str = "1,20,300"
for match in string.gmatch(str , "%d+") do
	print(match) --> "1", "20", "300"
end

Or if you want just one match, string.match.

string.gsub works just fine
anyways i need a way to squash repeated spaces into one space

string.gsub replaces all matches with the replacement, which isn’t what you wanted. If you’re doing that to use string.split to split every space, then you should just use string.gmatch instead.

final resulting script for those looking in the future

local strin = "1week 2"
local empty = {}
for match in string.gmatch(strin , "%d+") do
	table.insert(empty, match)
end
print(empty)

You could have just marked @averlst’s post as the solution…

ok but im evil and wicked :smiling_face_with_horns::smiling_face_with_horns::smiling_face_with_horns::smiling_face_with_horns::smiling_face_with_horns: (also it actually uses a table)