lua-patterns

Find substring between specific characters

安稳与你 提交于 2021-01-28 21:59:11
问题 I am unfamiliar with Lua language and a I would like your help. I am trying to receive some values through POST and the values is something like that: pwd = password ssid = ssid_name swstat={string.find(payload,"pwd=")} swstat1={string.find(payload,"ssid=")} if swstat[2]~=nil then pass=string.sub(payload,swstat[2]+1,#payload) ssid=string.sub(payload,swstat1[2]+1,#payload) print("Password: "..pass) print("SSID: "..ssid) end The actual result of the above code is ( am sending through a web

Find substring between specific characters

被刻印的时光 ゝ 提交于 2021-01-28 21:39:29
问题 I am unfamiliar with Lua language and a I would like your help. I am trying to receive some values through POST and the values is something like that: pwd = password ssid = ssid_name swstat={string.find(payload,"pwd=")} swstat1={string.find(payload,"ssid=")} if swstat[2]~=nil then pass=string.sub(payload,swstat[2]+1,#payload) ssid=string.sub(payload,swstat1[2]+1,#payload) print("Password: "..pass) print("SSID: "..ssid) end The actual result of the above code is ( am sending through a web

how to check if a word appears as a whole word in a string in Lua

≡放荡痞女 提交于 2020-01-28 10:00:28
问题 not sure how to check if a word appears as a whole word in a string, not part of a word, case sensitive. for example: Play is in strings Info Playlist Play pause but not in the strings Info Playlist pause Info NowPlay pause 回答1: Since there is no usual \b word boundary in Lua, you can make use of a frontier pattern %f . %f[%a] matches a transition to a letter and %f[%A] matches the opposite transition. %f[set] , a frontier pattern ; such item matches an empty string at any position such that

Lua pattern matching vs. regular expressions

我怕爱的太早我们不能终老 提交于 2020-01-09 06:04:14
问题 I'm currently learning lua. regarding pattern-matching in lua I found the following sentence in the lua documentation on lua.org: Nevertheless, pattern matching in Lua is a powerful tool and includes some features that are difficult to match with standard POSIX implementations. As I'm familiar with posix regular expressions I would like to know if there are any common samples where lua pattern matching is "better" compared to regular expression -- or did I misinterpret the sentence? and if

Print value from a lua table if pattern matches

◇◆丶佛笑我妖孽 提交于 2020-01-04 14:21:24
问题 Okay, so I just recently got into lua and find myself stuck with the following: I have the function peripheral.getNames() (which is a custom function) it will return a table with the structure key,value, whereas key is always a number and starts from 1 and value will be what the function finds (it searches for devices connected to it) In my example it creates a table which looks like this 1 herp 2 derp 3 monitor_1 4 morederp I can print the values with the following local pgn = peripherals

Split a string using string.gmatch() in Lua

夙愿已清 提交于 2020-01-01 00:45:33
问题 There are some discussions here, and utility functions, for splitting strings, but I need an ad-hoc one-liner for a very simple task. I have the following string: local s = "one;two;;four" And I want to split it on ";" . I want, eventually, go get { "one", "two", "", "four" } in return. So I tried to do: local s = "one;two;;four" local words = {} for w in s:gmatch("([^;]*)") do table.insert(words, w) end But the result (the words table) is { "one", "", "two", "", "", "four", "" } . That's

Modifying a character in a string in Lua

大憨熊 提交于 2019-12-30 06:37:28
问题 Is there any way to replace a character at position N in a string in Lua. This is what I've come up with so far: function replace_char(pos, str, r) return str:sub(pos, pos - 1) .. r .. str:sub(pos + 1, str:len()) end str = replace_char(2, "aaaaaa", "X") print(str) I can't use gsub either as that would replace every capture, not just the capture at position N. 回答1: Strings in Lua are immutable. That means, that any solution that replaces text in a string must end up constructing a new string

Why isn't this string.match consistently working?

孤街醉人 提交于 2019-12-24 03:27:59
问题 I am writing an addon for the game World of Warcraft (WoW). It uses lua, with a library of functions specific to the game. I am checking whether a substring can be found in a string. The substring in question is given by the variable ItemType, which in this case contains the string "Two-Handed Swords" . The string in which I am checking is given by a table entry and contains "One-Handed Axes, One-Handed Swords, Two-Handed Axes, Two-Handed Swords, Bows, Crossbows, Guns, Wands, Mail, Plate,

Lua XML extract from pattern

一个人想着一个人 提交于 2019-12-21 21:27:56
问题 An application is sending my script an Stream like this one: <?xml version="1.0" encoding="UTF-8"?> <root> <aRootChildNode> <anotherChildNode> <?xml version="1.0"> <TheNodeImLookingFor> ... content ... </TheNodeImLookingFor> </anotherChildNode> </aRootChildNode> </root> I want to extract the TheNodeImLookingFor section. So far, got: data = string.match(Stream, "^.+\<TheNodeImLookingFor\>.+\<\/TheNodeImLookingFor\>.+$") Pattern is recognized in the Stream, but it doesn't extract the node and

Sorting IPv4 address with port number

匆匆过客 提交于 2019-12-14 03:57:33
问题 I'm trying to alter an IP address string that has a port number in it so to sort a table, an example IP string: IP = "120.88.66.99:075" I can remove the . 's and the : with: IP = string.gsub(IP,'%W',"") and that gives me 120886699075 but I would like to change the only : to a . so it gives me 120886699.075 Edit: Actually what I wanted is not working as it does not take in to account the number of numbers between the .'s so what I woulds like is a way to sort the ip in the given format so a