lua-patterns

Corona string.find() : Finding “.”

旧巷老猫 提交于 2019-12-13 04:32:11
问题 I'm trying to split a string into two parts, which is divided by a '.' character. But string.find() function cant handle that I have this kind of string local test = "345345.57573" I tried local start = string.find( test, "." ) local start = string.find( test, "\." ) local start = string.find( test, "(%w+).(%w+)" ) But none of them worked. String.find() always return 1 which is false. What might be the problem? Edit: I also tried to use gsub and change . with another character but it didn't

Unexpected lua pattern matching result

这一生的挚爱 提交于 2019-12-12 12:19:21
问题 For the following code: local function getParentPath(_path) pattern = "(.+)([/\\][/\\])(.+)" i,j,k = string.match(path,pattern) return i,j,k end print(getParentPath(path)) For path = "C://data//file.text", I get: C://data // file.text But For path = "C:\data\file.text", I get: nil nil nil I am looking for a pattern which woks for both. Any suggestions? 回答1: The problem is that the first .+ matches greedily and grabs all up to the last \ and then backtracks. Then, one \ can be matched with [\\

Lua gmatch odd characters (Slovak alphabet)

元气小坏坏 提交于 2019-12-12 11:04:10
问题 I am trying to extract the characters from a string of a word in Slovak. For example, the word for "TURTLE" is "KORYTNAČKA". However, it skips over the "Č" character when I try to extract it from the string: local str = "KORYTNAČKA" for c in str:gmatch("%a") do print(c) end --result: K,O,R,Y,T,N,A,K,A I am reading this page and I have also tried just pasting in the string itself as a set, but it comes up with something weird: local str = "KORYTNAČKA" for c in str:gmatch("["..str.."]") do

Lua string.gsub with Multiple Patterns

独自空忆成欢 提交于 2019-12-12 10:49:18
问题 I am working on renaming the Movie titles that has unwanted letters. The string.gsub can replace a string with "" nil value but I have around 200 string patterns that need to be replaces with "". Right now I have to string.gsub for every pattern. I was thinking is there is a way to put all the string patterns in to single string.gsub line. I have searched around the web for the solution but still didn't got anything. The movie title is like this B.A.Pass 2013 Hindi 720p DvDRip CROPPED AAC

lua: pattern matching and extracting a phone number

孤者浪人 提交于 2019-12-11 14:30:20
问题 I am having trouble crafting a function that has the following requirements in Lua: Takes a string phone_number and 2-digit country_code as input. phone_number has the form {1 || ""}{ country_code }{10 or 11-digit mobile number} I need as output the 10 or 11-digit mobile number. Example I/O: phone_number = "552234332344", country_code = "55" => "2234332344" phone_number = "15522343323443", country_code = "55" => "22343323443" Thanks! 回答1: Try "(1?)(%d%d)(%d+)" . Using this with your examples:

Pattern in lua with anchors not matching

允我心安 提交于 2019-12-11 11:26:16
问题 Why this does not match? I want to match the exact pattern 2 letters followed by 3 numbers s = "dd123" for w in string.gmatch(s, "^%a%a%d%d%d$") do print(w) matched = true end 回答1: If you just want to see if a string matches a pattern, use string.match instead. s = "dd123" print(string.match(s, "^%a%a%d%d%d$")) -- dd123 string.gmatch is for finding all matches in a string, and doesn't work correctly with ^ and $ . 来源: https://stackoverflow.com/questions/26000428/pattern-in-lua-with-anchors

How to isolate non english words separated by spaces in Lua?

自闭症网瘾萝莉.ら 提交于 2019-12-11 01:49:18
问题 I have this string "Hello there, this is some line-aa." how to slice it into an array like this? Hello there, this is some line-aa. this is what I have tried so far function sliceSpaces(arg) local list = {} for k in arg:gmatch("%w+") do print(k) table.insert(list, k) end return list end local sentence = "مرحبا يا اخوتي" print("sliceSpaces") print(sliceSpaces(sentence)) this code works for English text , but not for arabic, how can I make it work for arabic too? 回答1: Lua strings are sequences

Pattern ^u.meta(\.|$) not working as expected

跟風遠走 提交于 2019-12-11 01:41:40
问题 I have this pattern: ^u.meta(\.|$) EXPECTED BEHAVIOUR ^u.meta(\.|$) will match all the roles like: u.meta u.meta.admin u.meta.admin.system u.meta.* Where as it should not match something like below: u.meta_admin u.meta_admin_system I have tested this pattern with https://regex101.com/ online regexp tester . PROBLEM: I have to implement this pattern with lua script. but getting invalid escape sequence near '\.' : -- lua script > return string.match("u.meta.admin", '^u.meta(\.|$)') stdin:1:

Lua string.gsub() by '%s' or '\n' pattern

不羁的心 提交于 2019-12-11 01:04:15
问题 English isn't my mother tongue,so it's a little hard to describe the question. I wanna to get 'd=40' in str by lua string.gsub(),but there's some problem. ------code below--- local str = [==[ -- a=10 - -b=20 --c=30 d=40 ]==] local pat1 = [=[%s[%s]]=] local pat2 = [=[\n[%s]]=] str:gsub(pat1, function(s) print("pat1>>" .. s) end) --pat1>>d=40 str:gsub(pat2, function(s) print("pat2<<" .. s) end) --not match local re1,_ = str:gsub("\n","$") local re2,_ = str:gsub("%s","$") print(re1) --a=10$- -b

Optional Group Capture with Lua Pattern Matching

爷,独闯天下 提交于 2019-12-10 15:44:22
问题 I am trying to parse chemical formulas in Lua using simple pattern matching. However, I do not know how to specify a capture group as being optional. Here is the pattern I have come up with: pattern = "(%u%l*)(%d*)" The first group captures the atomic symbol (i.e. "H", "He", etc..) and the second group captures the number of that atom in the molecule. This value is usually an integer value, but if it is 1, it is often omitted, such as in: formula = "C2H6O" When I attempt to do a global match,