Lua:将字符串拆分为单词,除非被引用。
2015-2-23 3:11:45
收藏:0
阅读:62
评论:3
我有以下代码来分割一个字符串中的空格:
text = "I am 'the text'"
for string in text:gmatch("%S+") do
print(string)
end
结果:
I
am
'the
text'
但我需要做到这一步:
I
am
the text --[[是的,没有引号]]
我该如何做到这一点?
编辑: 补充问题,我的想法是将一个程序的参数传递给另一个程序。这是我正在处理的拉请求,目前正在审核中:https://github.com/mpv-player/mpv/pull/1619
点赞
用户107090
text = [[I am 'the text' and '' here is "another text in quotes" and this is the end]]
local e = 0
while true do
local b = e+1
b = text:find("%S",b)
if b==nil then break end
if text:sub(b,b)=="'" then
e = text:find("'",b+1)
b = b+1
elseif text:sub(b,b)=='"' then
e = text:find('"',b+1)
b = b+1
else
e = text:find("%s",b+1)
end
if e==nil then e=#text+1 end
print("["..text:sub(b,e-1).."]")
end
尝试一下:
text = [[我是文本'这里是""引号中的另一个文本"这就是结尾"]]
local e = 0
while true do
local b = e+1
b = text:find("%S",b)
if b==nil then break end
if text:sub(b,b)=="'" then
e = text:find("'",b+1)
b = b+1
elseif text:sub(b,b)=='"' then
e = text:find('"',b+1)
b = b+1
else
e = text:find("%s",b+1)
end
if e==nil then e=#text+1 end
print("["..text:sub(b,e-1).."]")
end
2015-02-23 01:27:29
用户2726734
Lua模式不足以处理这个任务,这里提供一个改编自Lua Lexer的LPEG解决方案,可以处理单引号和双引号。
local lpeg = require 'lpeg'
local P, S, C, Cc, Ct = lpeg.P, lpeg.S, lpeg.C, lpeg.Cc, lpeg.Ct
local function token(id, patt) return Ct(Cc(id) * C(patt)) end
local singleq = P "'" * ((1 - S "'\r\n\f\\") + (P '\\' * 1)) ^ 0 * "'"
local doubleq = P '"' * ((1 - S '"\r\n\f\\') + (P '\\' * 1)) ^ 0 * '"'
local white = token('whitespace', S('\r\n\f\t ')^1)
local word = token('word', (1 - S("' \r\n\f\t\""))^1)
local string = token('string', singleq + doubleq)
local tokens = Ct((string + white + word) ^ 0)
input = [["This is a string" 'another string' these are words]]
for _, tok in ipairs(lpeg.match(tokens, input)) do
if tok[1] ~= "whitespace" then
if tok[1] == "string" then
print(tok[2]:sub(2,-2)) -- cut off quotes
else
print(tok[2])
end
end
end
输出:
This is a string
another string
these
are
words
2015-02-23 13:14:09
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
可能有一些巧妙的解析方式,但另一种方法可能是通过跟踪简单状态并基于检测到引用片段合并片段。可以尝试这种方式:
local text = [[I "am" 'the text' and "some more text with '" and "escaped \" text"]] local spat, epat, buf, quoted = [=[^(['"])]=], [=[(['"])$]=] for str in text:gmatch("%S+") do local squoted = str:match(spat) local equoted = str:match(epat) local escaped = str:match([=[(\*)['"]$]=]) if squoted and not quoted and not equoted then buf, quoted = str, squoted elseif buf and equoted == quoted and #escaped % 2 == 0 then str, buf, quoted = buf .. ' ' .. str, nil, nil elseif buf then buf = buf .. ' ' .. str end if not buf then print((str:gsub(spat,""):gsub(epat,""))) end end if buf then print("Missing matching quote for "..buf) end这将打印:
I am the text and some more text with ' and escaped \" text更新以处理混合和转义引号。更新以删除引号。更新以处理带引号的单词。