RegEx用于字母数字文本字符串,最多可使用特殊模式

时间:2019-04-18 19:05:07

标签: python regex

我有一个具有特定格式的字符串列表,只需要这些元素的一部分即可。

输入

my_list = ['The Price Is Right S47E141 720p WEB x264-W4F', 'Breakthrough-The Ideas That Changed the World S01E01 480p x264-mSD',
'The Kid Who Would Be King 2019 DVDR-JFKDVD', 'American Housewife S03E18 Phone Free Day 1080p AMZN WEB-DL DDP5 1 H 264-NTb',
'VICE News Tonight 2019 04 16 720p AMZN WEB-DL DDP2 0 H 264-monkee','The Flash 2014 S05E18 Godspeed 720p AMZN WEB-DL DDP5 1 H 264-NTb',
'The Rachel Maddow Show 2019 04 16 720p MNBC WEB-DL AAC2 0 x264-BTW','Lets Make A Deal 2009 S10E142 XviD-AFG']

RegEx尝试:

try:
    try:
        def get_rls(t):
            w = re.match(".*\d{4} \d{2} \d{2} ", t)
            # w = re.match(".*S\d+E\d+", t)
            if not w: raise Exception("Error For Regular Expression")
            return w.group(0)

        regular_case = [my_list ]
        for w in regular_case:
            Regular_part = get_rls(w)
            print(">>>> Movie Regular Part contains Year/Mon/Day : ", Regular_part)         
    except:
        try:
            def get_rls(t):
                # w = re.match(".*\d ", t)
                w = re.match(".*S\d+E\d+", t)
                if not w: raise Exception("Error For Regular Expression")
                return w.group(0)


            regular_case = [my_list ]
            for w in regular_case:
                Regular_part = get_rls(w)
                print(">>>> Movie Regular Part contains S0E0 : ", Regular_part)             

        except:
            def get_rls(t):
                w = re.match(".*\d{4} ", t)
                # w = re.match(".*S\d+E\d+", t)
                if not w: raise Exception("Error For Regular Expression")
                return w.group(0)


            regular_case = [my_list ]
            for w in regular_case:
                Regular_part = get_rls(w)
                print(">>>> Movie Regular Part contains Year : ", Regular_part)

except:
    print(">>>> Weard Release Name! Pass the Regular part ")
    Regular_part = my_list 

问题是,我的正则表达式代码只能获取一个元素并决定使用哪个RegEx有用并打印正则表达式,而我需要RegEx代码能够获取列表并处理每个单个元素,例如get首先确定哪一个是好的。

最好的结果应该类似于以下列表:

my_list = ['The Price Is Right S47E141', 'Breakthrough-The Ideas That Changed the World S01E01',
'The Kid Who Would Be King 2019 DVDR-JFKDVD', 'American Housewife S03E18 ',
'VICE News Tonight 2019 04 16','The Flash 2014 S05E18',
'The Rachel Maddow Show 2019 04 16 ','Lets Make A Deal 2009 S10E142']

1 个答案:

答案 0 :(得分:0)

This RegEx并非完全正确的答案,但也许它将帮助您找出处理文本输入的一般方法。也许RegEx并不是解决此问题的最佳方法:

pythreejs

enter image description here

^.+?(?:[SE0-9]+)|(?:\s[A-Z]{4}\-[A-Z]{1,})|(?:.+[0-9]{4}\s[0-9]{2}\s[0-9]{2})|(?:\s[SE0-9]{6,10}) 模式和SE模式对于此RegEx很简单。您可能会遇到的问题是随机年份,例如date20142009,您可能会考虑这些年份。

相关问题