Lua string.gsub有多个模式

时间:2014-08-13 07:25:19

标签: string lua lua-patterns

我正在重新命名包含不需要的字母的电影标题。 string.gsub可以用""替换字符串。零值,但我有大约200个字符串模式需要替换为""。

现在我必须为每个模式string.gsub。我在想是否有办法将所有字符串模式放入单string.gsub行。我已经在网上搜索了解决方案,但仍然没有得到任何结果。

电影标题如下B.A.Pass 2013 Hindi 720p DvDRip CROPPED AAC x264 RickyKT 我想删除2013Hindi720pDvDRipCROPPEDAACx264等额外字符,RickyKT

4 个答案:

答案 0 :(得分:8)

你可以传递给string.gsub一个表作为第三个参数:

local movie = "B.A.Pass 2013 Hindi 720p DvDRip CROPPED AAC x264 RickyKT"
movie = movie:gsub("%S+", {["2013"] = "", ["Hindi"] = "", ["720p"] = "", 
                       ["DvDRip"] = "", ["CROPPED"] = "", ["AAC"] = "", 
                       ["x264"] = "", ["RickyKT"] = ""})

print(movie)

答案 1 :(得分:2)

将所有模式放在表中,然后枚举表,为每个模式调用string.gsub()

str = "B.A.Pass 2013 Hindi 720p DvDRip CROPPED AAC x264 RickyKT"

patterns = {"pattern1", "pattern2", "pattern3"}
for i,v in ipairs(patterns) do
    str = string.gsub(str, v, "")
end

这需要对string.gsub()进行多次调用,但代码应该比进行大量string.gsub()调用更具可维护性。

答案 2 :(得分:0)

为了避免在每个新条目的表上写入键和值,我会编写一个函数来处理数字索引表(模式是值)。

这样我就不需要为每个新模式编写{["pattern_n"] = ""}

例如:

PATTERNS = {"2013", "Hindi", "720p", "DvDRip", "CROPPED", "AAC", "x264", "RickyKT"}
function replace(match)
    local ret = nil
    for i, v in ipairs(PATTERNS) do
        if v:find(match) then
            ret = ""
        end
    end
    return ret
end


local movie = "B.A.Pass 2013 Hindi 720p DvDRip CROPPED AAC x264 RickyKT"
movie = movie:gsub("%S+", replace)

print(movie)

答案 3 :(得分:0)

您可以通过一个简单的函数来执行此操作,这样就不必每次都为每个字符串编写代码,也不必仅将string.gsub和所需的字符串替换值放入

功能

local large_name = "B.A.Pass 2013 Hindi 720p DvDRip CROPPED AAC x264 RickyKT"

function clean_name(str)
  local v = string.gsub(str, "(.-)%s([%(%[']?%d%d%d?%d?[%)%]]?)%s*(.*)", "%1")
  return v
end

print(clean_name(large_name))

仅string.gsub可获得值

local large_name = "B.A.Pass 2013 Hindi 720p DvDRip CROPPED AAC x264 RickyKT"
local clean_name = string.gsub(large_name, "(.-)%s([%(%[']?%d%d%d?%d?[%)%]]?)%s*(.*)", "%1")

print(clean_name)

替换模式将第一个值(电影名称)用空格分隔并打印,并将年份标识为第二个值,以避免标题出错,因此不必放置所有值可以存在于电影名称中并且可以避免很多误报

我添加了测试功能以测试不同的电影名称

local testing = {"Whiplash 2014 [1080p]",
"Anon (2018) [WEBRip] [1080p] [YTS.AM]",
"Maze Runner The Death Cure 2018 [WEBRip] [1080p] [YTS.AM]",
"12 Strong [2018] [WEBRip] [1080p] [YTS.AM]",
"Kingsman The Secret Service (2014) [1080p]",
"The Equalizer [2014] [1080p]",
"Annihilation 2018 [WEBRip] [1080p] [YTS.AM]",
"The Shawshank Redemption '94",
"Assassin's Creed 2016 HC 720p HDRip 850 MB - iExTV",
"Captain Marvel (2019) [WEBRip] [1080p] [YTS.AM]",}

for k,v in pairs(testing) do
  local result = string.gsub(v, "(.-)%s([%(%[']?%d%d%d?%d?[%)%]]?)%s*(.*)", "%1")
  print(result)
end

输出:

Whiplash
Anon
Maze Runner The Death Cure
12 Strong
Kingsman The Secret Service
The Equalizer
Annihilation
The Shawshank Redemption
Assassin's Creed
Captain Marvel