Z
Z
zysyl2014-01-13 20:49:54
Lua
zysyl, 2014-01-13 20:49:54

What is the correct way to use regexps and parentheses in lua?

Why doesn't this design work?

local querycreate = "create user 'dddqwd123_dwqd'@'localhost'"
local create, usercreate, username, userhost = querycreate:match("^(%w+)%s+(%w+)%s+\'(%w)\'@\'(%w)\'$")
print(string.format("query: %s", querycreate))
print(string.format(" var create = %s \n var usercreate = %s \n var username = %s \n var userhost = %s", create, usercreate, username, userhost))

When executed, I get:
query: create user 'dddqwd123_dwqd'@'localhost'
var create = nil
var usercreate = nil
var username = nil
var userhost = nil
If the regexp is changed to ("^(%w+)%s+(%w+)%s+ "), then I get:
var create = create
var usercreate = user
var username = nil
var userhost =
nil w+)%s+(%w+) @ (%w+)$, then I get the output I need.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
avalak, 2014-01-13
@zysyl

querycreate:match("^(%w+)%s+(%w+)%s+'([%w_]+)'@'(%w+)'$")

tyts tyts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question