3

I have quite simple question, but my google research did not help.. I am pretty new to Lua, so..

I have string "XXXX_YYYYYY_zzzzzz" stored in local variable and I want to parse it and get 3 new local variables. Should I use string.find?

local str_ = "XXXX_YYYYY_zzzzzz"    
local first_, second_, third_ = strind.find(str_, "^(%w+)_(%w+)_(%w+)$")
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
LuaLuaLua
  • 33
  • 4

1 Answers1

5

Use string.match instead:

local str_ = "XXXX_YYYYY_zzzzzz"    
local first_, second_, third_ = str_:match "^([^_]+)_([^_]+)_([^_]+)$"

Have a look at the string library on lua-users wiki.

string.find would additionally return the indexes where the matched substring was located/found. These two (start and end) indices are not useful to your case, which is why string.match would be a better tool.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183