You're friend is thinking of regular expressions. Lua patterns are similar, but different. % is the correct escape character. Your pattern should be %(%(.-%)%). The - is similar to * in that it matches any number of the preceding sequence, but while * tries to match as many characters as...
The error is in the line data[i] = line This line makes data[i] a string variable which cannot have other strings indexed to it. Change that line to: data[i] = {} and everything works fine....
There may be ways to do this with clever parsing, but an alternative way may be to keep track of a simple state and merge fragments based on detection of quoted fragments. Something like this may work: local text = [[I "am" 'the text' and "some more text with '"...
arrays,string,lua,lua-patterns
You can store the result into a table: local t = {string.match(array[i], pattern)} if #t ~= 0 then words = t end end The return value of parse_string is a table now: local result = (parse_strings (a, 'alarm dentist (%w+) (%w+)')) for k, v in ipairs(result) do print(k, v) end...
lua,time-complexity,lua-patterns
Yes, lua patterns can take exponential time. Try running: string.find('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?' .. 'a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') They can still run reasonably fast if you keep the patterns simple though so I would try testing some real examples on your own data....
string,sdk,lua,corona,lua-patterns
You can use string.gsub with lua string-patterns: string.gsub("this is a !a joke !/a! haha", "!a %a+ !/a!", "cheer") If need be you can even capture the string that's between !a: local str = "this is a !a joke !/a! haha" str:gsub ("!a (%a+) !/a!", "sad %1") Which after substitution gives...
Since regular matching is greedy, you just need to match anything until you see . (don't forget to escape it): print(string.match("That.Awkward.Moment.2014.720p.BluRay.x264.YIFY.srt", '(.+)%.(.+)')) will print That.Awkward.Moment.2014.720p.BluRay.x264.YIFY srt ...
Try: print(string.match(str,"%[COLOR r;255|g;255|b;0%](.-)%[/COLOR%]")) Note that [,] are magic characters, so they need to be escaped....
Easiest is to do it separately: function roll(input) local min,high = string.match(input, '(%d+)-(%d+)') if min == nil then min = string.match(input, '(%d+)') end return min, high end print(roll '10') print(roll '10-100') ...
string,lua,pattern-matching,lua-patterns
In general, it's not a good idea to use pattern matching (either Lua pattern or regex) to extract XML. Use a XML parser. For this problem, you don't need to escape \ or <(even if you do, Lua pattern uses % to escape magic characters). And use brackets to get...
In this line: local s,e=string.find(line,"%w+ ",pos) -- ^ There is a whitespace in the pattern "%w+ ", so it matches a word followed by a whitespace. When you input, e.g, word1 word2 word3 and press Enter, word3 has no whitespace following it. There is no whitespace in the example of...
regex,lua,numbers,lua-patterns
The whole thing can be shortened to this one liner: local hh, mm, ss = dt:match "(%d%d?):(%d%d?):(%d%d?)" As for the error occuring in your tonumber, it is because gsub returns 2 values after its operation. First being the substituted sring and the second being a number. tonumber assumes the second...
To match on one of a set of characters you need to use the [set] notation1. So the following should do what you want and provide a slightly better indication of the failure: local p = string.find(text, '[&"#[email protected]%%!^*]'); if p then print("Invalid character found at "..p) end You could even...
string,lua,string-matching,gsub,lua-patterns
You could try like this. > f = "foo [\"12\"] bar" > x = string.gsub(f, "%[\"(%d+)\"%]", "[%1]") > print(f) foo ["12"] bar > print(x) foo [12] bar \d which matches any digits would be represented as %d in lua....
string,lua,pattern-matching,lua-patterns
Given: local s = [[5427 root 21620 S mpd /etc/mpd2.conf 5437 root 25660 S mpd]] The following pattern string.match(s,"(%d+)%s+root%s+%d+%s+S%s+mpd[%s]-$") returns: 5437 root 25660 S mpd whereas this: string.match(s,"(%d+%s+root%s+%d+%s+S%s+mpd[%s]%p?[%w%p]+)") returns: 5427 root 21620 S mpd /etc/mpd2.conf...
str:gsub( '§\\n(.)', '%1' ) should do what you want. This deletes the delimiter given that it is followed by another character, putting this character back into to string. Test code local str = { 'abc§\\ndef§\\nghi\\n', 'abc§\\ndef§\\nghi§\\n', 'abc§\\ndef§\\nghi§§\\n', } for i = 1, #str do print( ( str[ i ]:gsub( '§\\n(.)',...
string,lua,string-matching,lua-patterns,lua-5.1
I'm not sure the requirement you described are correct, but this logic follows your requirements: -- Only characters A-Z and numbers 0-9 and the - so long as it's not at the start or end of string. -- Be at least 3 characters excluding the extension. -- No spaces allowed...
local str = "importanttext1 has gathered importanttext2 from stackoverflow." local s1 = str:match("(.+)has") local s2 = str:match("gathered%s+(%S+)") print(s1) print(s2) Note that "%s" matches a whitespace character while "%S" matches a non-whitespace character....
Use the pattern "[?%s]*", which means zero or more of ? or whitespace characters. if text:gsub("[?%s]*", "") == "" then -- do something end ...
Well for one thing, you miss-spelled strawberry here: fruits = {apple = "green", orange = "orange", stawberry = "red"} You can also work with lua tables as sets, which means that nested loop searching for duplicates is unnecessary. It can be simplified to something like: fruits = {apple = "green",...
url,lua,pattern-matching,string-matching,lua-patterns
-- all characters allowed to be inside URL according to RFC 3986 but without -- comma, semicolon, apostrophe, equal, brackets and parentheses -- (as they are used frequently as URL separators) local string_with_URLs = [[ <a href="http://www.lua.org:80/manual/5.2/contents.html">L.ua 5.2</a> [url=127.0.0.1:8080]forum link[/url] [markdown link](https://74.125.143.101/search?q=Who+are+the+Lua+People%3F) auth link: ftp://user:[email protected]/path - not recognized yet ]]...
You need to escape . with %..