如何在Lua中从字符串中获取特定文本?
2017-2-4 16:10:58
收藏:0
阅读:42
评论:2
我想从发送到我的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("<" + ValueToSearch + ">", ValueToSearch);
Response = Response.Replace("</" + ValueToSearch + ">", 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("<", "");
returnValue = returnValue.Replace(">", "");
returnValue = returnValue.Replace("&", "");
}
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的新手
原文链接 https://stackoverflow.com/questions/42042266
点赞
stackoverflow用户6879826
下面是两个可以实现此功能的函数:
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
2017-02-04 19:17:23
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
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&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 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&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"); // ""