lua匹配标签后的所有内容

字符串如下: 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等。我已经尝试了许多方法,但没有令我满意的。也许有人有想法如何解决这个问题?

点赞
用户12514997
用户12514997

我们要在一个函数 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
2020-01-25 20:25:00