简体   繁体   English

优先考虑多个正则表达式模式

[英]Prioritize multiple regex patterns

I want to try a second pattern only if the first one didn't match.只有当第一个模式不匹配时,我才想尝试第二个模式。 The use of the Match object is the same in both cases. Match object 的使用在这两种情况下是相同的。 Is there any way to make it shorter?有什么办法可以让它变短吗?

for line in cli.splitlines():
    res = re.search('\s*(.*) (.*);', line)
    if res:
        pairs_found[res.groups()[0]] = res.groups()[1]
    else:
        res = re.search('(\w*) (\d*) {', line)
        if res:
            pairs_found[res.groups()[0]] = res.groups()[1]

The following precompiles the regexes into patterns and uses the behaviour of or to evaluate to the first match:以下将正则表达式预编译为模式,并使用or的行为来评估第一个匹配项:

pat1 = re.compile('\s*(.*) (.*);')
pat2 = re.compile('(\w*) (\d*) {')

# or forced into one line:
# pat1, pat2 = map(re.compile, ['\s*(.*) (.*);', '(\w*) (\d*) {'])

for line in cli.splitlines():
    res = pat1.search(line) or pat2.search(line)
    if res:
        pairs_found[res.groups()[0]] = res.groups()[1]

In Python >= 3.8, you can use an assignment expression to shorten it even more:在 Python >= 3.8 中,您可以使用赋值表达式来进一步缩短它:

    if res := (pat1.search(line) or pat2.search(line)):
        pairs_found[res.groups()[0]] = res.groups()[1]

Does this help?这有帮助吗?

for line in cli.splitlines():
    res = re.search('\s*(.*) (.*);', line)
    if not res: res = re.search('(\w*) (\d*) {', line)
    if res: pairs_found[res.groups()[0]] = res.groups()[1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM