如何使用模式来缩短匹配的字符串
How to use patterns to shorten only matching strings
我有一个程序可以获取外设类型列表,匹配它们以查看它们是否是有效类型,然后在它们有效时执行特定于类型的代码。
但是,某些类型可以共享其名称的一部分,唯一的区别是它们的层级,我想将它们与 table 有效外围设备中列出的基本类型相匹配,但我可以'弄清楚如何使用模式来匹配它们,而模式不会为所有不匹配的内容返回 nil
。
下面是演示我的问题的代码:
connectedPeripherals = {
[1] = "tile_thermalexpansion_cell_basic_name",
[2] = "modem",
[3] = "BigReactors-Turbine",
[4] = "tile_thermalexpansion_cell_resonant_name",
[5] = "monitor",
[6] = "tile_thermalexpansion_cell_hardened_name",
[7] = "tile_thermalexpansion_cell_reinforced_name",
[8] = "tile_blockcapacitorbank_name"
}
validPeripherals = {
["tile_thermalexpansion_cell"]=true,
["tile_blockcapacitorbank_name"]=true,
["monitor"]=true,
["BigReactors-Turbine"]=true,
["BigReactors-Reactor"]=true
}
for i = 1, #connectedPeripherals do
local periFunctions = {
["tile_thermalexpansion_cell"] = function()
--content
end,
["tile_blockcapacitorbank_name"] = function()
--content
end,
["monitor"] = function()
--content
end,
["BigReactors-Turbine"] = function()
--content
end,
["BigReactors-Reactor"] = function()
--content
end
}
if validPeripherals[connectedPeripherals[i]] then periFunctions[connectedPeripherals[i]]() end
end
如果我尝试 运行 那样,所有热膨胀单元都不会被识别为有效外围设备,如果我添加模式匹配语句,它适用于热膨胀单元,但是 returns nil 其他所有内容并导致异常。
我如何做一个匹配语句,只有 returns 匹配的东西的缩短字符串和 returns 不匹配的东西的原始字符串?
这可能吗?
根据the comment, you can also use string.find
查看与您的外设名称匹配的类型:
for i,v in ipairs(connectedPeripherals) do
local Valid = CheckValidity(v)
if Valid then Valid() end
end
其中,CheckValidity
将 return 来自 validPeripherals
的密钥:
function CheckValidity( name )
for n, b in pairs(validPeripherals) do
if name:find( n ) then return n end
end
return false
end
如果短版本不包含来自 Lua 模式的任何特殊字符,您可以使用以下内容:
long = "tile_thermalexpansion_cell_basic_name"
result = long:match("tile_thermalexpansion_cell") or long
print(result) -- prints the shorter version
result = long:match("foo") or long
print(result) -- prints the long version
我有一个程序可以获取外设类型列表,匹配它们以查看它们是否是有效类型,然后在它们有效时执行特定于类型的代码。
但是,某些类型可以共享其名称的一部分,唯一的区别是它们的层级,我想将它们与 table 有效外围设备中列出的基本类型相匹配,但我可以'弄清楚如何使用模式来匹配它们,而模式不会为所有不匹配的内容返回 nil
。
下面是演示我的问题的代码:
connectedPeripherals = {
[1] = "tile_thermalexpansion_cell_basic_name",
[2] = "modem",
[3] = "BigReactors-Turbine",
[4] = "tile_thermalexpansion_cell_resonant_name",
[5] = "monitor",
[6] = "tile_thermalexpansion_cell_hardened_name",
[7] = "tile_thermalexpansion_cell_reinforced_name",
[8] = "tile_blockcapacitorbank_name"
}
validPeripherals = {
["tile_thermalexpansion_cell"]=true,
["tile_blockcapacitorbank_name"]=true,
["monitor"]=true,
["BigReactors-Turbine"]=true,
["BigReactors-Reactor"]=true
}
for i = 1, #connectedPeripherals do
local periFunctions = {
["tile_thermalexpansion_cell"] = function()
--content
end,
["tile_blockcapacitorbank_name"] = function()
--content
end,
["monitor"] = function()
--content
end,
["BigReactors-Turbine"] = function()
--content
end,
["BigReactors-Reactor"] = function()
--content
end
}
if validPeripherals[connectedPeripherals[i]] then periFunctions[connectedPeripherals[i]]() end
end
如果我尝试 运行 那样,所有热膨胀单元都不会被识别为有效外围设备,如果我添加模式匹配语句,它适用于热膨胀单元,但是 returns nil 其他所有内容并导致异常。
我如何做一个匹配语句,只有 returns 匹配的东西的缩短字符串和 returns 不匹配的东西的原始字符串?
这可能吗?
根据the comment, you can also use string.find
查看与您的外设名称匹配的类型:
for i,v in ipairs(connectedPeripherals) do
local Valid = CheckValidity(v)
if Valid then Valid() end
end
其中,CheckValidity
将 return 来自 validPeripherals
的密钥:
function CheckValidity( name )
for n, b in pairs(validPeripherals) do
if name:find( n ) then return n end
end
return false
end
如果短版本不包含来自 Lua 模式的任何特殊字符,您可以使用以下内容:
long = "tile_thermalexpansion_cell_basic_name"
result = long:match("tile_thermalexpansion_cell") or long
print(result) -- prints the shorter version
result = long:match("foo") or long
print(result) -- prints the long version