Lua.g string with multiple patterns

I'm working on renaming movie titles that have junk mail. string.gsub can replace the string with the value "nil, but I have about 200 string patterns that need to be replaced with" ".

Now I have to string.gsub for each template. I thought there was a way to put all the string patterns on a single string string.gsub . I searched the Internet for a solution but received nothing.

The name of the movie is similar to BAPass 2013 Hindi 720p DvDRip CROPPED AAC x264 RickyKT and I want to remove additional characters like 2013 , Hindi , 720p , DvDRip , CROPPED , AAC , x264 , RickyKT .

+5
source share
4 answers

You can go to the string.gsub table as the third argument as follows:

 local movie = "BAPass 2013 Hindi 720p DvDRip CROPPED AAC x264 RickyKT" movie = movie:gsub("%S+", {["2013"] = "", ["Hindi"] = "", ["720p"] = "", ["DvDRip"] = "", ["CROPPED"] = "", ["AAC"] = "", ["x264"] = "", ["RickyKT"] = ""}) print(movie) 
+8
source

Put all the templates in a table, and then list the table by calling string.gsub() for each template:

 str = "BAPass 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 

This will require many calls to string.gsub() , but the code should be much more convenient to serve than to have many calls to string.gsub() .

+2
source

To avoid writing keys and values ​​in the table for each new record, I would write a function to process the table with numerical indexing (templates were values).

Thus, I do not need to write {["pattern_n"] = ""} for each new pattern.

Example:

 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 = "BAPass 2013 Hindi 720p DvDRip CROPPED AAC x264 RickyKT" movie = movie:gsub("%S+", replace) print(movie) 
0
source

You can do this with a simple function so that you don't have to write code every time for each line, or just put string.gsub and a value to replace string.gsub you string.

Function:

 local large_name = "BAPass 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)) 

Only string.gsub for value

 local large_name = "BAPass 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) 

The replacement template puts the first value (the name of the film) separated by a space and prints it, and also defines the year as the second value to avoid heading errors, so there is no need to place all the values ​​that may exist in the name of the film and avoid many false positives

I add a test function to check different movie titles

 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 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 

Exit:

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

Source: https://habr.com/ru/post/1200194/


All Articles