如何从 Lua 中的字符串中获取某些文本?

How can I get certain text from string in Lua?

我想提取从发送到我的 NodeMCU 的 HTTP 请求中收到的 POST 参数。我怎样才能做到这一点?我在考虑 C# 中的以下代码。如何在 Lua 中实施?

我的 C# 代码:

// Response = "<action>Play</action><speed>1</speed><blah>lol</blah>"
// ValuetoSearch = "action"
public static string GetInformationFromResponse(string Response, string ValueToSearch, bool RemoveHtmlCharacters = true) {
            string returnValue = "";
            if (RemoveHtmlCharacters) {
                Response = Response.Replace("<" + ValueToSearch + ">", ValueToSearch);
                Response = Response.Replace("</" + ValueToSearch + ">", ValueToSearch);
                Response = Response.Replace("&lt;" + ValueToSearch + "&gt;", ValueToSearch);
                Response = Response.Replace("&lt;/" + ValueToSearch + "&gt;", ValueToSearch);
            }

            // Response = "actionPlayaction<Speed>1</Speed><blah>lol</blah>"

            int indexOfWord = Response.IndexOf(ValueToSearch); // indexOfWord = 0
            int start = indexOfWord + ValueToSearch.Length; // start = 6
            int end = Response.Length - indexOfWord - 1; // 47
            int totalLength = Response.Length; // 48
            string newPositionInfo = "";

            if (indexOfWord == -1) {
                return "";
            } else {
                newPositionInfo = Response.Substring(start, totalLength - start); // newPositionInfo = "Playaction<Speed>1</Speed><blah>lol</blah>"
                indexOfWord = newPositionInfo.IndexOf(ValueToSearch); // indexOfWord = 4
                returnValue = newPositionInfo.Substring(0, indexOfWord); // returnValue = "Play"
                if (RemoveHtmlCharacters) {
                    returnValue = returnValue.Replace("&lt;", "");
                    returnValue = returnValue.Replace("&gt;", "");
                    returnValue = returnValue.Replace("&amp;", "");
                }
                return returnValue; // "Play"
            }
        }

这段代码的用法如下: - 我想获得单词 "action" 之间的所有内容。 - 我有一段包含单词 "action".

的文本
string largeText = "<action>Play</action><speed>1</speed><blah>blah</blah>"
string wordToSearch = "action"
string value1 = GetInformationFromResponse(largeText, "action");
string value2 = GetInformationFromResponse(largeText, "speed");
string value3 = GetInformationFromResponse(largeText, "blah");
// Value 1 = "Play"
// Value 2 = "1"
// Value 3 = "blah"

但是我如何在 Lua(在我的 NodeMCU 上)完成同样的事情?

注意:Lua 和 NodeMCU

新手
function GetInformationFromResponse(response, tag)
   return
      ((response:match((("<@>(.-)</@>"):gsub("@",tag))) or "")
      :gsub("&(%w+);", {lt = "<", gt = ">", amp = "&"}))
end

local text = "<action>Play</action><speed>1</speed><blah>blah&amp;blah</blah>"
local value1 = GetInformationFromResponse(text, "action"); -- "Play"
local value2 = GetInformationFromResponse(text, "speed");  -- "1"
local value3 = GetInformationFromResponse(text, "blah");   -- "blah&blah"
local value4 = GetInformationFromResponse(text, "foo");    -- ""

这里有几个函数可以做到这一点:

function get_text (str, init, term)
   local _, start = string.find(str, init)
   local stop = string.find(str, term)
   local result = nil
   if _ and stop then
      result = string.sub(str, start + 1, stop - 1)
   end
   return result
end

function get_tagged (str, tag)
   local open_tag = "<" .. tag ..">"
   local close_tag = "</" .. tag .. ">"
   local _, start = string.find(str, open_tag)
   local stop = string.find(str, close_tag)
   local result = nil
   if _ and stop then
      result = string.sub(str, start + 1, stop - 1)
   end   
   return result
end

交互示例:

> largeText = "<action>Play</action><speed>1</speed><blah>blah</blah>"
> -- Using get_text()
> print(get_text(largeText, "<action>", "</action>"))
Play
> -- Using get_tagged()
> print(get_tagged(largeText, "action"))
Play
> print(get_tagged(largeText, "speed"))
1
> print(get_tagged(largeText, "blah"))
blah
> print(get_tagged(largeText, "oops"))
nil