Я хочу извлечь параметры POST, полученные из HTTP-запроса, который был отправлен на мой NodeMCU. Как я могу это сделать? Я думал о следующем коде на C#. Как реализовать это в Lua?Как я могу получить определенный текст из строки в 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"
}
}
Использование этого кода выглядит следующим образом: - Я хочу, чтобы получить все, что между словом «действия». - У меня есть текст, содержащий слово «действие».
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
Спасибо! Работает как шарм! – Johnnybossboy