C# 如何从Lua中的字符串中获取特定文本?
我想提取从HTTP请求接收到的POST参数,该请求被发送到我的NodeMCU。我怎样才能做到这一点?我在考虑C#中的以下代码。如何在Lua中实现这一点 我的C#代码:C# 如何从Lua中的字符串中获取特定文本?,c#,lua,nodemcu,C#,Lua,Nodemcu,我想提取从HTTP请求接收到的POST参数,该请求被发送到我的NodeMCU。我怎样才能做到这一点?我在考虑C#中的以下代码。如何在Lua中实现这一点 我的C#代码: //Response=“play1ll” //ValuetoSearch=“操作” 公共静态字符串GetInformationFromResponse(字符串响应,字符串值搜索,boolRemoveHtmlCharacters=true){ 字符串returnValue=“”; 如果(删除HtmlCharacters){ Resp
//Response=“play1ll”
//ValuetoSearch=“操作”
公共静态字符串GetInformationFromResponse(字符串响应,字符串值搜索,boolRemoveHtmlCharacters=true){
字符串returnValue=“”;
如果(删除HtmlCharacters){
Response=Response.Replace(“,ValueToSearch”);
Response=Response.Replace(“,ValueToSearch”);
响应=响应。替换(“+ValueToSearch+”,ValueToSearch);
响应=响应。替换(“/”+ValueToSearch+”,ValueToSearch);
}
//Response=“ActionPlayAction1OL”
int indexoford=Response.IndexOf(ValueToSearch);//indexoford=0
int start=indexoford+ValueToSearch.Length;//start=6
int end=Response.Length-indexoford-1;//47
int totalLength=响应.长度;//48
字符串newPositionInfo=“”;
if(indexOfWord==-1){
返回“”;
}否则{
newPositionInfo=Response.Substring(start,totalLength-start);//newPositionInfo=“playaction1ol”
indexoford=newPositionInfo.IndexOf(ValueToSearch);//indexoford=4
returnValue=newPositionInfo.Substring(0,indexoford);//returnValue=“Play”
如果(删除HtmlCharacters){
returnValue=returnValue.Replace(“,”);
returnValue=returnValue.Replace(“,”);
returnValue=returnValue。替换(“&;”,“”);
}
返回值;/“播放”
}
}
此代码的用法如下所示:
-我想得到“行动”这个词之间的一切。
-我有一篇包含“行动”一词的文章
string largeText=“Play1blah”
字符串wordToSearch=“action”
字符串值1=GetInformationFromResponse(largeText,“操作”);
字符串值2=GetInformationFromResponse(largeText,“速度”);
字符串值3=GetInformationFromResponse(largeText,“blah”);
//Value 1=“播放”
//值2=“1”
//值3=“诸如此类”
但是我如何在Lua中(在我的NodeMCU上)完成同样的事情呢
注:Lua和NodeMCU上的新手函数GetInformationFromResponse(响应,标记)
返回
((响应:匹配((“(.-”):gsub(“@”,tag)))或“”)
:gsub(&(%w+),{lt=”“,amp=“&”}))
结束
本地text=“Play1blah&;blah”
本地值1=GetInformationFromResponse(文本,“操作”);--“玩”
本地值2=从响应获取信息(文本,“速度”);--"1"
本地值3=GetInformation fromResponse(文本,“blah”);--“诸如此类”
本地值4=GetInformationFromResponse(文本,“foo”);--""
函数GetInformationFromResponse(响应,标记)
返回
((响应:匹配((“(.-”):gsub(“@”,tag)))或“”)
:gsub(&(%w+),{lt=”“,amp=“&”}))
结束
本地text=“Play1blah&;blah”
本地值1=GetInformationFromResponse(文本,“操作”);--“玩”
本地值2=从响应获取信息(文本,“速度”);--"1"
本地值3=GetInformation fromResponse(文本,“blah”);--“诸如此类”
本地值4=GetInformationFromResponse(文本,“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
函数get_text(str、init、term)
local u,start=string.find(str,init)
本地停止=string.find(str,term)
局部结果=零
如果停止,那么
结果=string.sub(str,start+1,stop-1)
结束
返回结果
结束
函数get_taged(str,tag)
本地打开标签=“”
本地关闭标签=“”
local u,start=string.find(str,open_标记)
本地停止=string.find(str,close_标记)
局部结果=零
如果停止,那么
结果=string.sub(str,start+1,stop-1)
结束
返回结果
结束
样本交互:
> 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
>largeText=“Play1blah”
>--使用get_text()
>打印(获取文本(LarGetText,“,”))
玩
>--使用get_taged()
>打印(标记(大文本,“操作”))
玩
>打印(标记(大文本,“速度”))
1.
>打印(带标签(大文本,“废话”))
废话
>打印(标记(大文本,“oops”))
无
这里有几个函数可以实现这一点:
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
函数get_text(str、init、term)
local u,start=string.find(str,init)
本地停止=string.find(str,term)
局部结果=零
如果停止,那么
结果=string.sub(str,start+1,stop-1)
结束
返回结果
结束
函数get_taged(str,tag)
本地打开标签=“”
本地关闭标签=“”
local u,start=string.find(str,open_标记)
本地停止=string.find(str,close_标记)
局部结果=零
如果停止,那么
结果=string.sub(str,start+1,stop-1)
结束
返回结果
结束
样本交互:
> 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
>largeText=“Play1blah”
>--使用get_text()
>打印(获取文本(LarGetText,“,”))
玩
>--使用get_taged()
>打印(标记(大文本,“操作”))
玩
>打印(标记(大文本,“速度”))
1.
>打印(带标签(大文本,“废话”))
废话
>打印(标记(大文本,“oops”))
无
谢谢!工作起来很有魅力!谢谢工作起来很有魅力!