lua 匹配字符串中标记后的所有内容

lua match everything after a tag in a string

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

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

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

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

%s - 匹配单个 space 字符

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

这给我们留下了像“$TEMPLATE templatename manufacturer”这样的字符串 我们想将 $TEMPLATE 提取到它自己的变量中以验证它,因此我们使用 string.match(string, pattern) 来仅 return 字符串中模式找到的值。

好的:编辑:这是一个综合示例,应提供您正在寻找的一切。

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