lua匹配标签后的所有内容
2021-3-29 17:29:54
收藏:0
阅读:142
评论:1
字符串如下: TEMPLATES="!$TEMPLATE templatename manufacturer model mode\n$TEMPLATE MacQuantum Wash Basic\n$$MANUFACTURER Martin\n$$MODELNAME Mac Quantum Wash\n$$MODENAME Basic\n"
我获取没有标签的字符串的方式如下:
local sentence=""
for word in string.gmatch(line,"%S+") do
if word ~= tag then
sentence=sentence .. word.." "
end
end
table.insert(tagValues, sentence)
E(tag .." --> "..sentence)
并且我得到的输出如下:
``` $$MANUFACTURER --> Martin $$MODELNAME --> Mac Quantum Wash ... ...
```但是这不是我想要的方式。我想首先找到以$TEMPLATE标记开头的块,以检查是否为正确的块。我逐行读取一个文件,这里有许多这样的块。然后我必须获取所有标有双重$的标记:$$MODELNAME等。我已经尝试了许多方法,但没有令我满意的。也许有人有想法如何解决这个问题?
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 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 代码?

我们要在一个函数
string.gmatch中使用 Lua 模式(类似于正则表达式,但不同)来创建一个循环。解释:
for match in string.gmatch(string, pattern) do print(match) end是一个迭代函数,将遍历string中每个pattern的实例。我将使用的模式是%$+%w+%s[^\n]+%$+- 至少有一个 $ 字符($是一个特殊字符,所以需要%来转义),+表示1个或多个。如果只需要标签的数据,则可以对一个(“% $”)进行匹配,但是我们需要有关有多少美元符号的信息,因此我们将其留在其中。%w+- 匹配任何字母数字字符,出现在一排中。%s- 匹配一个单独的空格字符。[^\n]+- 匹配任何不是 '\n' 的字符(^表示反转),出现在一排中。一旦函数遇到了 \n,它就会在该匹配上执行循环并重复该过程。
这让我们有了类似于 "$TEMPLATE templatename manufacturer" 的字符串。
我们想要将 $TEMPLATE 提取到自己的变量中进行验证,因此我们使用
string.match(string, pattern)仅返回找到的字符串中模式的值。好的:编辑:这是一个综合性的示例,应该提供您所寻找的一切内容。
templates = "!$TEMPLATE templatename manufacturer model mode\n$TEMPLATE MacQuantum Wash Basic\n$$MANUFACTURER Martin\n$$MODELNAME Mac Quantum Wash\n$$MODENAME Basic\n" local data = {} for match in string.gmatch(templates, "%$+%w+%s[^\n]+") do --finds the pattern given in the variable 'templates' --this function assigns certain data to tags inside table t, which goes inside data. local t = {} t.tag = string.match(match, '%w+') --the tag (stuff that comes between a $ and a space) t.info = string.gsub(match, '%$+%w+%s', "") --value of the tag (stuff that comes after the `$TEMPLATE `. Explanation: %$+ one or more dollar signs $w+ one or more alphanumeric characters $s a space. Replace with "" (erase it) _, t.ds = string.gsub(match, '%$', "") --This function emits two values, the first one is garbage and we don't need (hence a blank variable, _). The second is the number of $s in the string). table.insert(data, t) end for _,tag in pairs(data) do --iterate over every table of data in data. for key, value in pairs(tag) do print("Key:", key, "Value:", value) --this will show you data examples (see output) end print("-------------") end print('--just print the stuff with two dollar signs') for key, data in pairs(data) do if data.ds == 2 then --'data' becomes a subtable in table 'data', we evaluate how many dollar signs it recognized. print(data.tag) end end print("--just print the MODELNAME tag's value") for key, data in pairs(data) do if data.tag == "MODELNAME" then --evaluate the tag name. print(data.info) end end输出:
Key: info Value: templatename manufacturer model mode Key: ds Value: 1 Key: tag Value: TEMPLATE ------------- Key: info Value: MacQuantum Wash Basic Key: ds Value: 1 Key: tag Value: TEMPLATE ------------- Key: info Value: Martin Key: ds Value: 2 Key: tag Value: MANUFACTURER ------------- Key: info Value: Mac Quantum Wash Key: ds Value: 2 Key: tag Value: MODELNAME ------------- Key: info Value: Basic Key: ds Value: 2 Key: tag Value: MODENAME ------------- --just print the stuff with two dollar signs MANUFACTURER MODELNAME MODENAME --just print the MODELNAME tag's value: Mac Quantum Wash