LUA string.format 中的正则表达式
The regex in string.format of LUA
我使用 LUA 的 string.format(str, regex) 来获取一些关键字。
local RICH_TAGS = {
"texture",
"img",
}
--\[((img)|(texture))=
local START_OF_PATTER = "\[("
for index = 1, #RICH_TAGS - 1 do
START_OF_PATTER = START_OF_PATTER .. "(" .. RICH_TAGS[index]..")|"
end
START_OF_PATTER = START_OF_PATTER .. "("..RICH_TAGS[#RICH_TAGS].."))"
function RichTextDecoder.decodeRich(str)
local result = {}
print(str, START_OF_PATTER)
dump({string.find(str, START_OF_PATTER)})
end
产出
hello[img=123] \[((texture)|(img))
dump from: [string "utils/RichTextDecoder.lua"]:21: in function 'decodeRich'
"<var>" = {
}
输出的意思是:
str = 你好[img=123]
START_OF_PATTER = \[((纹理)|(img))
此正则表达式适用于某些在线正则表达式工具。但它在 LUA 中什么也找不到。
我的代码中使用有什么错误吗?
您不能在 Lua 中使用正则表达式。使用 Lua 的 string patterns 来匹配字符串。
见How to write this regular expression in Lua?
尝试dump({str:find("\%[%("))})
还要注意这个循环:
for index = 1, #RICH_TAGS - 1 do
START_OF_PATTER = START_OF_PATTER .. "(" .. RICH_TAGS[index]..")|"
end
将省略 RICH_TAGS
的最后一个元素,我想这不是你的本意。
编辑:
But what I want is to fetch several specific word. For example, the
pattern can fetch "[img=" "[texture=" "[font=" any one of them. With
the regex string I wrote in my question, regex can do the work. But
with Lua, the way to do the job is write code like string.find(str,
"[img=") and string.find(str, "[texture=") and string.find(str,
"[font="). I wonder there should be a way to do the job with a single
pattern string. I tryed pattern string like "%[%a*=", but obviously it
will fetch a lot more string I need.
您不能将多个特定单词与单个模式匹配,除非它们以特定顺序出现在该字符串中。您唯一可以做的就是将构成这些单词的所有字符放入 class,但您可能会冒着找到可以从这些字母构建的任何单词的风险。
通常您会用单独的模式匹配每个单词,或者您匹配任何单词并使用查找 table 来检查匹配项是否是您的单词之一。
所以基本上你可以在 Lua.
的几行中完成正则表达式库会做的事情
我使用 LUA 的 string.format(str, regex) 来获取一些关键字。
local RICH_TAGS = {
"texture",
"img",
}
--\[((img)|(texture))=
local START_OF_PATTER = "\[("
for index = 1, #RICH_TAGS - 1 do
START_OF_PATTER = START_OF_PATTER .. "(" .. RICH_TAGS[index]..")|"
end
START_OF_PATTER = START_OF_PATTER .. "("..RICH_TAGS[#RICH_TAGS].."))"
function RichTextDecoder.decodeRich(str)
local result = {}
print(str, START_OF_PATTER)
dump({string.find(str, START_OF_PATTER)})
end
产出
hello[img=123] \[((texture)|(img))
dump from: [string "utils/RichTextDecoder.lua"]:21: in function 'decodeRich'
"<var>" = {
}
输出的意思是:
str = 你好[img=123]
START_OF_PATTER = \[((纹理)|(img))
此正则表达式适用于某些在线正则表达式工具。但它在 LUA 中什么也找不到。 我的代码中使用有什么错误吗?
您不能在 Lua 中使用正则表达式。使用 Lua 的 string patterns 来匹配字符串。
见How to write this regular expression in Lua?
尝试dump({str:find("\%[%("))})
还要注意这个循环:
for index = 1, #RICH_TAGS - 1 do
START_OF_PATTER = START_OF_PATTER .. "(" .. RICH_TAGS[index]..")|"
end
将省略 RICH_TAGS
的最后一个元素,我想这不是你的本意。
编辑:
But what I want is to fetch several specific word. For example, the pattern can fetch "[img=" "[texture=" "[font=" any one of them. With the regex string I wrote in my question, regex can do the work. But with Lua, the way to do the job is write code like string.find(str, "[img=") and string.find(str, "[texture=") and string.find(str, "[font="). I wonder there should be a way to do the job with a single pattern string. I tryed pattern string like "%[%a*=", but obviously it will fetch a lot more string I need.
您不能将多个特定单词与单个模式匹配,除非它们以特定顺序出现在该字符串中。您唯一可以做的就是将构成这些单词的所有字符放入 class,但您可能会冒着找到可以从这些字母构建的任何单词的风险。
通常您会用单独的模式匹配每个单词,或者您匹配任何单词并使用查找 table 来检查匹配项是否是您的单词之一。
所以基本上你可以在 Lua.
的几行中完成正则表达式库会做的事情