How to extract from a string

I would like to extract the ID and the UserName from the string returned from the Kwikset door lock:

UserID=“world”, UserName=“Lua”

Currently I pass this string back to my website and parse it there but would like to do it in Lua.

I have been experimenting with string.match, etc. but I’m not great at regex patterns.

I would like to extract world and Lua. In php I look for UserID=" and " for the userId, UserName=" and " for the username.

Can anyone help to help me reduce my head banging!?

Thanks!!

Maybe split it on "-character?

[code]
– splits a string by a pattern
– returns an array of the pieces

	function string:split(delimiter)
		local result = { }
		local from  = 1
		local delim_from, delim_to = string.find( self, delimiter, from  )
		while delim_from do
			table.insert( result, string.sub( self, from , delim_from-1 ) )
			from  = delim_to + 1
			delim_from, delim_to = string.find( self, delimiter, from  )
		end
		table.insert( result, string.sub( self, from  ) )
		return result
	end
	[/code]
str = 'UserID="world", UserName="Lua"'
print("Value for str:" ..  str)
for k,v  in string.gmatch(str, '([%w]+)="([%w]+)"') do
   print("Key:" .. k .. " Value:" ..  v);
end

When executed will result in:

Value for str:UserID="world", UserName="Lua"
Key:UserID Value:world
Key:UserName Value:Lua

[quote=“shicks3, post:1, topic:175132”]I would like to extract the ID and the UserName from the string returned from the Kwikset door lock:
UserID=“world”, UserName=“Lua”[/quote]

A more targeted version than hek’s or RichardTSchaefer’s (i.e., you won’t learn as much from mine but you’ll get what you want quicker):

str='UserID="world", UserName="Lua"'
i,n = str:match('^UserID="(.-)", UserName="(.-)"$')
-- use i (UserID) and n (UserName).

Thank you everyone for the replies. I learned a few things!!

I ended up with:

local i = userCode:match('UserID="(.-)"') local n = userCode:match('UserName="(.-)"')

I couldn’t make the combined match work, probably because I wasn’t matching the original string perfectly.

Thanks again!!