Extract number from string

[code]local http = require(“socket.http”)
http.TIMEOUT = 5

local status, userCode = luup.inet.wget(“http://192.168.1.153/arduino/analog/3”,5)
–userCode=‘Pin A2 reads analog 400’

local i = userCode:match('Pin (.-) ')
local nString = userCode:match(‘analog (.-)’)
local n = tonumber(nString)

if n > 500
then
luup.variable_set(“urn:upnp-org:serviceId:VContainer1”,“Variable1”,“” … n … “”,312)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“1” },161)
end
[/code]
With this code I can extract A2 but not the number 400 in the end.
I assume it’s because there is no stop character available:
userCode:match(‘analog (.-)[glow=red,2,300]_[/glow]’)

But how can I do it?

Btw. Will this work with a variable?
local n = tonumber(nString)

Try this as a search string:

a,b = userCode: match "Pin%s(%w*)[^%d]*(%d*)"

giving a = “A2”, b = “400”.

No need, really, to explicitly convert from a string to a number since Lua does these coercions for you.

If you’re going to do much more of this you really need to read up on Lua [url=http://www.lua.org/pil/contents.html]http://www.lua.org/pil/contents.html[/url]