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


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))

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


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

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


1 Like