Python正则表达式多模式,提取正确的组

时间:2015-08-27 00:15:32

标签: python regex regex-group

我已经在Python中编写了一个正则表达式,它应该用数字搜索season / s和episode / e。正如你在我的代码中看到的那样,我支持所有类型的模式来寻找我想要的东西。

import re

episode = re.compile(r"""(?:s|season)(?:\s)(\d+)(?:e|x|episode|\n)(?:\s)(\d+)| # s 01e 02
                        (?:s|season)(\d+)(?:e|x|episode|\n)(?:\s)(\d+)| # s01e 02
                        (?:s|season)(?:\s)(\d+)(?:e|x|episode|\n)(\d+)| # s 01e02
                        (?:s|season)(\d+)(?:e|x|episode|\n)(\d+)| # s01e02
                        (?:s|season)(\d+)(?:.*)(?:e|x|episode|\n)(\d+)| # s01 random123 e02
                        (?:s|season)(?:\s)(\d+)(?:.*)(?:e|x|episode|\n)(?:\s)(\d+)| # s 01 random123 e 02
                        (?:s|season)(?:\s)(\d+)(?:.*)(?:e|x|episode|\n)(\d+)| # s 01 random123 e02
                        (?:s|season)(\d+)(?:.*)(?:e|x|episode|\n)(?:\s)(\d+) # s01 random123 e 02
                        """, re.VERBOSE)

test="Hello seinfeld season 01episode 22 foo bar"

match = re.search(episode, test)
print match.group(1), match.group(2)

以下代码将按预期输出01 22。 但是,如果test字符串类似于:

,该怎么办?
test="Hello seinfeld season 01 episode 22 foo bar"

我如何知道要使用哪个群组?这意味着我不知道test对价值的影响。

编辑:也许我可以检查所有群组的价值,以及它是否真正使用该特定群组。但这似乎是一种错误的做法。

1 个答案:

答案 0 :(得分:1)

如何将每个正则表达式模式分解为一个列表,其中每个元素包含一个正则表达式模式?如果您需要添加/删除更多模式,这可以帮助您组织正则表达式模式,同时划分每个变体。您可能还想使用正则表达式命名组。

我修改了原始示例并进行了另外两项更改:1)单个模式,以及2)命名组,如下:

import re

pattern1 = re.compile(r"""(?:s|season)(?:\s)(?P<s>\d+)(?:e|x|episode|\n)(?:\s)(?P<ep>\d+) # s 01e 02""", re.VERBOSE)
pattern2 = re.compile(r"""(?:s|season)(?P<s>\d+)(?:e|x|episode|\n)(?:\s)(?P<ep>\d+) # s01e 02""", re.VERBOSE)
pattern3 = re.compile(r"""(?:s|season)(?:\s)(?P<s>\d+)(?:e|x|episode|\n)(?P<ep>\d+) # s 01e02""", re.VERBOSE)
pattern4 = re.compile(r"""(?:s|season)(?P<s>\d+)(?:e|x|episode|\n)(?P<ep>\d+) # s01e02""", re.VERBOSE)
pattern5 = re.compile(r"""(?:s|season)(?P<s>\d+)(?:.*)(?:e|x|episode|\n)(?P<ep>\d+) # s01 random123 e02""", re.VERBOSE)
pattern6 = re.compile(r"""(?:s|season)(?:\s)(?P<s>\d+)(?:.*)(?:e|x|episode|\n)(?:\s)(?P<ep>\d+) # s 01 random123 e 02""", re.VERBOSE)
pattern7 = re.compile(r"""(?:s|season)(?:\s)(?P<s>\d+)(?:.*)(?:e|x|episode|\n)(?P<ep>\d+) # s 01 random123 e02""", re.VERBOSE)
pattern8 = re.compile(r"""(?:s|season)(?P<s>\d+)(?:.*)(?:e|x|episode|\n)(?:\s)(?P<ep>\d+) # s01 random123 e 02""", re.VERBOSE)

patterns = [pattern1, pattern2, pattern3, pattern4, pattern5, pattern6, pattern7, pattern8 ]

test="Hello seinfeld season 01episode 22 foo bar"

for idx, p in enumerate(patterns):
    m = re.search(p, test)
    if m:
        print('MATCHED PATTERN: {}'.format( patterns[idx].pattern ) )
        print('    SEASON:  {}'.format( m.group('s')) )
        print('    EPISODE: {}'.format( m.group('ep')) )

输出:

MATCHED PATTERN: (?:s|season)(?:\s)(?P<s>\d+)(?:e|x|episode|\n)(?:\s)(?P<ep>\d+) # s 01e 02
    SEASON:  01
    EPISODE: 22

MATCHED PATTERN: (?:s|season)(?:\s)(?P<s>\d+)(?:.*)(?:e|x|episode|\n)(?:\s)(?P<ep>\d+) # s 01 random123 e 02
    SEASON:  01
    EPISODE: 22

当然,您需要添加一些额外的逻辑来选择要采用的匹配项(例如,您可以轻松选择第一个完整的匹配项),但至少这可以让您更加透明地了解正则表达式模式