disassemble strings

Posted by Couto on Sat 25 Feb 2012 07:48 PM — 3 posts, 13,520 views.

#0
Hi guys,

this weekend i got totally dragged into experimenting with ATCP and now i'm trying to disassemble a string and use only different parts of it.

the string i want to disassemble:
roomid = "raum/e4fdca13e0ce2426c14221923d632606 Nereid x=32 y=3"

Now i want to split it into following variables:
id = "raum/e4fdca13e0ce2426c14221923d632606"
area = "Nereid"
coordx = "x=32" --> maybe coordx = 32
coordy = "y=3" --> maybe coordy = 3

I tried the following 2 things which both didnt work. Either not usable for my intention or i'm not using it correctly.


  coordx = string.match(roomid, "x\=\d+")
  Note(coordx)
--> should search for x=32 in the string?

  for id, gegendname, coordx, coordy in string.gmatch(roomid, "(%raum\/\w+) (%w+) (%x\=\d+) (%y\=\d+)") do
  Note(id)
  Note(gegendname)
  Note(coordx)
  Note(coordy)
end
--> should disassemble the string completly in pieces?


Can anyone give me a short example how i can do this and maybe a explanation why my tries didnt suceed?

Edit: i forgot to mention that i only really need area, coordx and coordy
Amended on Sat 25 Feb 2012 07:52 PM by Couto
Australia Forum Administrator #1
You seem to be mixing a few styles here. The Lua regular expressions use %x rather than \x and so on.

This breaks up your string:

roomid = "raum/e4fdca13e0ce2426c14221923d632606 Nereid x=32 y=3"

local id, gegendname, coordx, coordy = string.match (roomid, "(raum/%x+) (%w+) x=(%d+) y=(%d+)") 

if id then
  Note(id)
  Note(gegendname)
  Note(coordx)
  Note(coordy)
else
  print ("no match")
end


Output:


raum/e4fdca13e0ce2426c14221923d632606
Nereid
32
3

#2
thanks a lot Nick