Lua字符串模式
2016-2-6 21:20:6
收藏:0
阅读:54
评论:3
这是我的代码:
local code = [[
local a = 1
local b = 2
]]
local y = 0
for Paragraph in string.gmatch(code,"[^\n]+") do
print(Paragraph)
for Word in string.gmatch(Paragraph, "[^ ]+") do
end
y = y + 1
end
问题是模式不能识别我的空段落,我该如何解决?
如果你运行代码,你会看到我想说的是什么
输出是:
local a = 1
local b = 2
而不是:
--空格
local a = 1
--空格
local b = 2
--空格
点赞
用户2858170
你没有得到空行是因为你寻找了\n的补集。如果你的行中只有\n,那么就没有补集,你就得不到匹配。
你可以使用这个模式:"\[^\n\]*\\n?" 来获得你想要的。这个模式匹配任何行。因此,任何不是\n且后跟0或1个实例的\n的东西或什么都不是
2016-02-06 21:56:13
用户5043289
你不会得到第一个空格因为 Lua 中的长字符串抛弃了第一个换行符。
你的问题在于,"[^\n]+" 匹配的是 至少一个 非换行符字符。空行不会匹配(两个换行符之间没有字符),因此它们不会显示出来。
现在你可以像这样将其更改为 "[^\n]*":
for Paragraph in string.gmatch(code,"[^\n]*") do
print("Line=", Paragraph)
for Word in string.gmatch(Paragraph, "[^ ]+") do
print ("Word=", Word)
end
end
但是这会有一个不同的问题:
Line= local a = 1
Word= local
Word= a
Word= =
Word= 1
Line=
Line=
Line= local b = 2
Word= local
Word= b
Word= =
Word= 2
Line=
Line=
空行会出现两次!
一个很有用的函数,每次迭代遍历字符串一行,如下:
function getlines (str)
local pos = 0
-- the for loop calls this for every iteration
-- returning nil terminates the loop
local function iterator (s)
if not pos then
return nil
end -- end of string, exit loop
local oldpos = pos + 1 -- step past previous newline
pos = string.find (s, "\n", oldpos) -- find next newline
if not pos then -- no more newlines, return rest of string
return string.sub (s, oldpos)
end -- no newline
return string.sub (s, oldpos, pos - 1)
end -- iterator
return iterator, str
end -- getlines
它处理了空行。现在你可以像这样编写代码(假设上面的函数在你的代码之前):
for Paragraph in getlines (code) do
print("Line=", Paragraph)
for Word in string.gmatch(Paragraph, "[^ ]+") do
print ("Word=", Word)
end
end
输出:
Line= local a = 1
Word= local
Word= a
Word= =
Word= 1
Line=
Line= local b = 2
Word= local
Word= b
Word= =
Word= 2
Line=
制作 Lua 模块
你可以将函数 getlines 转换成 Lua 模块,像这样:
getlines.lua
function getlines (str)
local pos = 0
-- the for loop calls this for every iteration
-- returning nil terminates the loop
local function iterator (s)
if not pos then
return nil
end -- end of string, exit loop
local oldpos = pos + 1 -- step past previous newline
pos = string.find (s, "\n", oldpos) -- find next newline
if not pos then -- no more newlines, return rest of string
return string.sub (s, oldpos)
end -- no newline
return string.sub (s, oldpos, pos - 1)
end -- iterator
return iterator, str
end -- getlines
return getlines
现在你需要“require”它:
require "getlines"
for Paragraph in getlines (code) do
print("Line=", Paragraph)
for Word in string.gmatch(Paragraph, "[^ ]+") do
print ("Word=", Word)
end
end
2016-02-06 22:12:17
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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 y,code = 0,[[ local a = 123 local b = 456 local c = "too much newlines because we're cool" ]] local i = 1 -- 为什么要从新行开始?好吧 local sigh = code:match("\n+") y = y + #sigh -- 调试打印新行 for i=1,y do print() end while true do local start,stop = code:find("[^\n]+",i) if not start then break end local Paragraph = code:sub(start,stop) -- 处理并解析段落 print("PARAGRAPH:",Paragraph) start,stop = code:find("\n+",stop+1) if not stop then break end y,i = y + stop - start + 1,stop+1 -- 打印新行以获得所需的输出效果 for i=2,stop-start+1 do print() end -- 从2开始,因为print(code:sub(...))已经打印了一个\n end -- 到达字符串结尾它会给你一个好看的输出结果:
PARAGRAPH: local a = 123 PARAGRAPH: local b = 456 PARAGRAPH: local c = "too much newlines because we're cool"在http://www.lua.org/cgi-bin/demo上测试过。