查找特定字符之间的子字符串

问题描述:

我不熟悉Lua语言,希望您的帮助.我正在尝试通过POST接收一些值,这些值是这样的:

I am unfamiliar with Lua language and a I would like your help. I am trying to receive some values through POST and the values is something like that:

pwd = password
ssid = ssid_name


swstat={string.find(payload,"pwd=")}
swstat1={string.find(payload,"ssid=")}
if swstat[2]~=nil then
    pass=string.sub(payload,swstat[2]+1,#payload)
    ssid=string.sub(payload,swstat1[2]+1,#payload)
    print("Password: "..pass)
    print("SSID: "..ssid)
end

以上代码的实际结果是(正在通过Web浏览器发送ssid = htc和password = fr):

The actual result of the above code is ( am sending through a web browser the ssid=htc and password=fr):

Password: fr
SSID: htc&pwd=fr

我想在一个字符串中仅包含密码,在另一个字符串中包含ssid.所以我的想法是使用以下逻辑搜索字符串:ssid =在字符串中找到介于"ssid =和(&"或字符串结尾)之间的子字符串pwd =在字符串中查找介于"pwd =和(&"或字符串结尾)之间的子字符串

I would like to have in a string the password only and in another string the ssid. So my idea is to search into string with the following logic: ssid = find in string the sub string which is between "ssid=" and ("&" or end of string) pwd = find in string the sub string which is between "pwd=" and ("&" or end of string)

由于 payload 是有效的查询字符串格式,因此您可以在表中存储相关信息:

Since payload is a valid query string format, you can store related information inside a table:

local tData = {}
for sParam, sValue in payload:gmatch "([^=&]+)=([^=&]+)" do
    tData[sParam:lower()] = sValue
end

现在,您将在 tData 表中获取信息:

Now, you'll have the information inside the tData table:

tData.pwd     -- will have fr
tData.ssid    -- will have htc