Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a Lua Patterns search correctly?

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?

like image 367
Willyan Fidelis Avatar asked Jul 20 '26 02:07

Willyan Fidelis


1 Answers

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
like image 55
Yu Hao Avatar answered Jul 24 '26 12:07

Yu Hao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!