0

字符串是这样的:TEMPLATES="!$TEMPLATE templatename 制造商型号模式\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 等的标签。我已经尝试了很多方法,但没有一个让我满意。也许有人知道如何解决它?

4

1 回答 1

1

我们将在函数 string.gmatch中使用 Lua模式(如 regex,但不同),这会创建一个循环。解释: for match in string.gmatch(string, pattern) do print(match) end是一个迭代函数,它将迭代patternin的每个实例string。我将使用的模式是%$+%w+%s[^\n]+

%$+- 至少 1 个文字 $($ 是一个特殊字符,因此需要 % 转义),+ 表示 1 个或更多。如果您只需要标签的数据,您可以只匹配一个 ("%$"),但我们需要有关有多少 $ 的信息,因此我们将其保留。

%w+- 匹配任何字母数字字符,与连续出现的一样多。

%s- 匹配单个空格字符

[^\n]+- 匹配任何不是 '\n' 的内容(^ 表示反转),与连续出现的一样多。一旦函数命中 \n,它就会在匹配项上执行循环并重复该过程。

这给我们留下了像“$TEMPLATE 模板名称制造商”这样的字符串。我们想将 $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-25T20:25:00.523 回答