Get first number from string, get numbers in string table

Ex for desired result I:
“abcd123efgh525” ->123

Ex for desired result II:
“abcd123efgh525” → {123,525}

I am invalid with strings. How do you recommend approaching this?

1 Like

For the first one you would do

("abcd123efgh525"):match("%d+")

since it returns 1 or more occurrences of digits “in a row”. So at the character "e" it would stop.

For the second one you can do

local digits = { }

for occurrence in ("abcd123efgh525"):gmatch("%d+") do
    table.insert(digits, tonumber(occurrence))
end

Since you go through all occurrences of digits in a row.

4 Likes

This does not support decimal numbers. I found out you can do that with:

local numberValue = tonumber("string1.25string":match("[%.%d]+"))
--returns 1.25

from Lua.org; Programming in Lua : 20.2.

1 Like