As the title says... Chat on...

User avatar
By jankop
#6859 Can someone would advise please? I need to find ín string the first group of digits in length of 1,2 or three digits for convert it to number.
But what I wrote, does not work as I need. I'm novice in Lua.
Code: Select allstrnum=string.find(queryString,"%d+")
SetPower=tonumber(string.sub(queryString,strnum))


what do I need:
"assd45sdsf12dasf45" > "45"
"wfj85694sgsd" > "856"
"welfg1a2485ab546" > "1"

Thanks
User avatar
By ThomasW
#6862
Code: Select allpattern="[0-9][0-9]?[0-9]?"

function test(str)
    print(str .. ":")
    print(string.match(str,pattern))
end

test("assd45sdsf12dasf45")
test("wfj85694sgsd")
test("welfg1a2485ab546")
test("nodigit")
test("000atstart")
test("atend9876")
test("atend9")

Results:
Code: Select allassd45sdsf12dasf45:
45
wfj85694sgsd:
856
welfg1a2485ab546:
1
nodigit:
nil
000atstart:
000
atend9876:
987
atend9:
9

.. hope I didn't miss any edge-case. Beware of the 'nil' result when there's no digit in the query-string.
Thomas