如何在 Lua 中撤销 string.gmatch 对字符串特定部分的操作
2019-3-18 23:4:29
收藏:0
阅读:119
评论:2
所以我正在使用 Lua 并通过空格分割字符串来编写某种子语言。我正在尝试使其不分割括号内的任何内容,我已经到达了可以检测是否存在括号的阶段。但我希望反转括号内字符串的 gmatching,因为我希望保留其中的字符串。
local function split(strng)
local __s={}
local all_included={}
local flag_table={}
local uncompiled={}
local flagged=false
local flagnum=0
local c=0
for i in string.gmatch(strng,'%S+') do
c=c+1
table.insert(all_included,i)
if(flagged==false)then
if(string.find(i,'%('or'%['or'%{'))then
flagged=true
flag_table[tostring(c)]=1
table.insert(uncompiled,i)
print'flagged'
else
table.insert(__s,i)
end
elseif(flagged==true)then
table.insert(uncompiled,i)
if(string.find(i,'%)' or '%]' or '%}'))then
flagged=false
local __=''
for i=1,#uncompiled do
__=__ .. uncompiled[i]
end
table.insert(__s,__)
print'unflagged'
end
end
end
return __s;
end
这是我的分割代码
点赞
用户7552
作为对 Uli 答案的一种类似但略有不同的方法,我会首先按括号进行拆分。 然后可以将奇数字段按空格拆分:
split = require("split") -- https://luarocks.org/modules/telemachus/split
split__by_parentheses = function(input)
local fields = {}
local level = 0
local field = ""
for i = 1, #input do
local char = input:sub(i, i)
if char == "(" then
if level == 0 then
-- add non-parenthesized field to list
fields[#fields+1] = field
field = ""
end
level = level + 1
end
field = field .. char
if char == ")" then
level = level - 1
assert(level >= 0, 'Mismatched parentheses')
if level == 0 then
-- add parenthesized field to list
fields[#fields+1] = field
field = ""
end
end
end
assert(level == 0, 'Mismatched parentheses')
fields[#fields+1] = field
return fields
end
input = " this is a string (containg some (well, many) annoying) parentheses and should be split. The string contains double spaces. What should be done? And what about trailing spaces? "
fields = split__by_parentheses(input)
for i, field in ipairs(fields) do
print(("%d\t'%s'"):format(i, field))
if i % 2 == 1 then
for j, word in ipairs(split.split(field)) do
print(("\t%d\t%s"):format(j, word))
end
end
end
输出结果:
1 ' this is a string '
1
2 this
3 is
4 a
5 string
6
2 '(containg some (well, many) annoying)'
3 ' parentheses and should be split. The string contains double spaces. What should be done? And what about trailing spaces? '
1
2 parentheses
3 and
4 should
5 be
6 split.
7 The
8 string
9 contains
10 double
11 spaces.
12 What
13 should
14 be
15 done?
16 And
17 what
18 about
19 trailing
20 spaces?
21
2019-03-19 15:14:43
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

我根本就不会使用
gmatch。local input = "这是一个字符串(包含一些(好吧,很多)讨厌的)括号,并应该被拆分。该字符串包含双空格。应该做什么?以及尾随空格呢?" local pos = 1 local words = {} local last_start = pos while pos <= #input do local char = string.byte(input, pos) if char == string.byte(" ") then table.insert(words, string.sub(input, last_start, pos - 1)) last_start = pos + 1 elseif char == string.byte("(") then local depth = 1 while depth ~= 0 and pos + 1 < #input do local char = string.byte(input, pos + 1) if char == string.byte(")") then depth = depth - 1 elseif char == string.byte("(") then depth = depth + 1 end pos = pos + 1 end end pos = pos + 1 end table.insert(words, string.sub(input, last_start)) for k, v in pairs(words) do print(k, "'" .. v .. "'") end输出:
1 '' 2 '这' 3 '是' 4 '一个' 5 '字符串' 6 '(包含一些(好吧,很多)讨厌的)' 7 '括号' 8 '并' 9 '应该' 10 '被' 11 '拆分。' 12 '该' 13 '字符串包含双空格。应该做什么?以及尾随空格呢?'关于尾随空格和其他类似问题的思考留给读者作为练习。我试图展示一些可能存在的问题。此外,我仅考虑了一种括号,因为我不想思考
this (string} should be ]parsed。哦,如果嵌套括号不是一个问题:上面的大部分代码都可以替换为一个调用
string.find(input, ")", pos, true)来查找闭合括号。请注意,您不能像在您的代码中尝试的那样使用或或并模式。
"%(" or "%["等于"%("。Lua 将按从左到右的顺序解释该表达式。
"%("是一个真值,Lua 将该表达式简化为"%(",逻辑上与完整表达式相同。因此,
string.find(i,'%('or'%['or'%{')只能在i中找到(。