How can I get certain text from string in Lua? [duplicate]

一个人想着一个人 提交于 2019-12-23 01:41:35

问题


I want to extract the POST parameters received from a HTTP request, that was sent to my NodeMCU. How can I accomplish this? I was thinking about the following code in C#. How to implement this in Lua?

My code in 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"
            }
        }

The usage of this code is as follows: - I want to obtain everything between the word "action". - I have a text containing the word "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"

But how do I accomplish the same thing in Lua (on my NodeMCU)?

Note: newbie on Lua and NodeMCU


回答1:


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");    -- ""



回答2:


Here are a couple of functions that will do this:

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

Sample interaction:

> 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


来源:https://stackoverflow.com/questions/42042266/how-can-i-get-certain-text-from-string-in-lua

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!