3

I am trying to do a pattern search, but it doesn't work. I have this code:

vars = "CmdTurn.on=off/GetPar.pwd=true"

_GET = {}
for k, v in string.gmatch(vars, "(%w+)(%p+)(%w+)=(%w+)&*") do
  _GET[k] = v
  print(k..":"..v)
end

After run this code I hope see this result:

CmdTurn.on:off
GetPar.pwd:true

But it doesn't work. The wrong result that appears is this one:

CmdTurn:.
GetPar:.

Any one could help me?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • As far as I can tell, your problem is your using brackets to capture 4 items in the pattern, but only using 2 return variables (k and v). Try using variables a,b,c,d and see if it works. – warspyking Jan 01 '16 at 04:30

1 Answers1

1

There are multiple capture groups in the pattern (%w+)(%p+)(%w+)=(%w+)&*, so k and v gets the result of the first two captures, which is not what you want.

Try this:

for k, v in string.gmatch(vars, "(%w+%p+%w+)=(%w+&*)") do
  print(k..":"..v)
end
Yu Hao
  • 119,891
  • 44
  • 235
  • 294