lua-patterns

Lua: split string into words unless quoted

旧时模样 提交于 2019-12-10 13:33:46
问题 So I have the following code to split a string between whitespaces: text = "I am 'the text'" for string in text:gmatch("%S+") do print(string) end The result: I am 'the text' But I need to do this: I am the text --[[yep, without the quotes]] How can I do this? Edit: just to complement the question, the idea is to pass parameters from a program to another program. Here is the pull request that I am working, currently in review: https://github.com/mpv-player/mpv/pull/1619 回答1: There may be ways

What is the alternation operator in Lua patterns?

▼魔方 西西 提交于 2019-12-08 17:45:02
问题 In regex, | is used for alternation. What is the corresponding character in Lua patterns? 回答1: First, note that Lua patterns are not regular expressions; they are their own simpler matching language (with different benefits and drawbacks). Per the specification I've linked to above, and per this answer, there is no alternation operator in a Lua pattern. To get this functionality you'll need to use a more powerful Lua construct (like LPEG or a Lua Regex Library). 回答2: Another workaround is:

what are the use of parentheses in this Lua pattern?

巧了我就是萌 提交于 2019-12-07 17:03:41
问题 base string is: IP: 192.168.0.1 Passing that string to string.gmatch function(besides below patterns) will return the following results: pattern: IP: (%d+.%d+.%d+.%d+) -->192.168.0.1 pattern: IP: %d+.%d+.%d+.%d+ -->IP: 192.168.0.1 My question is that what are the meaning of those parentheses to the Lua pattern matching engine? Why by using the parentheses in the first pattern, the IP: string omitted but in the second pattern no? 回答1: Anything inside parentheses is a capture group; any part of

What is the neatest way to split out a Path Name into its components in Lua

ぐ巨炮叔叔 提交于 2019-12-06 17:12:41
问题 I have a standard Windows Filename with Path. I need to split out the filename, extension and path from the string. I am currently simply reading the string backwards from the end looking for . to cut off the extension, and the first \ to get the path. I am sure I should be able to do this using a Lua pattern, but I keep failing when it comes to working from the right of the string. eg. c:\temp\test\myfile.txt should return c:\temp\test\ myfile.txt txt Thank you in advance apologies if this

what are the use of parentheses in this Lua pattern?

血红的双手。 提交于 2019-12-05 19:53:04
base string is: IP: 192.168.0.1 Passing that string to string.gmatch function(besides below patterns) will return the following results: pattern: IP: (%d+.%d+.%d+.%d+) -->192.168.0.1 pattern: IP: %d+.%d+.%d+.%d+ -->IP: 192.168.0.1 My question is that what are the meaning of those parentheses to the Lua pattern matching engine? Why by using the parentheses in the first pattern, the IP: string omitted but in the second pattern no? Anything inside parentheses is a capture group ; any part of the input string matched by the part of the pattern in parentheses is captured and returned by match() and

What is the neatest way to split out a Path Name into its components in Lua

别来无恙 提交于 2019-12-04 22:47:04
I have a standard Windows Filename with Path. I need to split out the filename, extension and path from the string. I am currently simply reading the string backwards from the end looking for . to cut off the extension, and the first \ to get the path. I am sure I should be able to do this using a Lua pattern, but I keep failing when it comes to working from the right of the string. eg. c:\temp\test\myfile.txt should return c:\temp\test\ myfile.txt txt Thank you in advance apologies if this is a duplicate, but I could find lots of examples for other languages, but not for Lua. > return string

Split a string using string.gmatch() in Lua

感情迁移 提交于 2019-12-03 04:11:19
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 certainly not what I want. Now, as I remarked, there are some discussions here on splitting strings, but

Lua frontier pattern match (whole word search)

心已入冬 提交于 2019-12-02 03:17:40
问题 can someone help me with this please: s_test = "this is a test string this is a test string " function String.Wholefind(Search_string, Word) _, F_result = string.gsub(Search_string, '%f[%a]'..Word..'%f[%A]',"") return F_result end A_test = String.Wholefind(s_test,"string") output: A_test = 2 So the frontier pattern finds the whole word no problem and gsub counts the whole words no problem but what if the search string has numbers? s_test = " 123test 123test 123" B_test = String.Wholefind(s

Finding '.' with string.find()

那年仲夏 提交于 2019-12-01 00:33:31
问题 I'm trying to make a simple string manipulation: getting the a file's name, without the extension. Only, string.find() seem to have an issue with dots: s = 'crate.png' i, j = string.find(s, '.') print(i, j) --> 1 1 And only with dots: s = 'crate.png' i, j = string.find(s, 'p') print(i, j) --> 7 7 Is that a bug, or am I doing something wrong? 回答1: string.find() , by default, does not find strings in strings, it finds patterns in strings. More complete info can be found at the link, but here is

Modifying a character in a string in Lua

☆樱花仙子☆ 提交于 2019-11-30 20:32:18
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. RBerteig Strings in Lua are immutable. That means, that any solution that replaces text in a string must end up constructing a new string with the desired content. For the specific case of replacing a single character with some other