I've seen this question asked before where people want to get numbers from a string, and this is usually the answer:
text = "12..34abcd1.234abcd"
numbers = {}
for i = 1, string.len(text) do
local pointer = string.sub(text, i, i)
if tonumber(pointer) then
table.insert(numbers, tonumber(pointer))
end
end
print("{"..table.concat(numbers, ", ").."}")
...or something like that. That gives me each digit in the string. I want to get actual numbers from the string. I don't want {1, 2, 3, 4, 1, 2, 3, 4}, I want {12, 34, 1.234}. So, this is what I have:
pattern = "%d+%.*%d*"
text = "12..34abcd1.234abcd"
numbers = {}
for number in string.gmatch(text, pattern) do
if tonumber(number) then
table.insert(numbers, tonumber(number))
end
end
print("{"..table.concat(numbers, ", ").."}")
...and that gives me exactly what I want. However, I want a little challenge. The pattern checks for arbitrary amounts of decimals. "3..........5" would be matched, but that's not a number. That's why I added the check on line 6. How can I remove the check on line 6 and still be guaranteed that each match in any string can be a number?

5 comments
Why would you even need that check on line 6 if you know what's going to be matched will be a digit?
I'm not looking for digits, I'm looking for numbers. 645, 1028, 1337, 3.1415, etc. If "3...5" was in the string and the check wasn't there, the script would put "3...5" in the table of numbers.
Still, why would you even need the check if what's going to be matched will be a number as stated in your string pattern?
The pattern checks for an arbitrary amount of decimals. "3.........5" would be matched, but that's not a number.
Ah, I see.