在 Python 中重用正则表达式捕获组 [英] Re-use of a regular expression capture group in Python

查看:94
本文介绍了在 Python 中重用正则表达式捕获组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 python 代码确实有效,但正则表达式 'search' 计算了两次:

The following python code does work but the regular expression 'search' is evaluated twice:

    # my_string varies, it gets the following strings = ['%10s', 'comma%11d', 'comma%-6.2f', '%-8s'] in a loop
    output_string = '|'
    re_compiled_pat_int = re.compile(r'comma%(\d+)d')
    re_compiled_pat_fp  = re.compile(r'comma%-?(\d+)\.(\d+)f')
    re_compiled_pat_str = re.compile(r'%(-?\d+)s')

    if re_compiled_pat_int.search(my_string):
        output_string += f' %s{re_compiled_pat_int.search (my_string).group(1)}s |' # results in | %10s |"
    elif re_compiled_pat_fp.search(fld_format):
        output_string += f' %-{re_compiled_pat_fp.search(my_string).group(1)}s |'
    elif re_compiled_pat_str.search(my_string):
        output_string += f' %{re_compiled_pat_str.search(my_string).group(1)}s |'
    # 'output_string' becomes: '| %10s | %-11s | %-6s | %-8s |'

如您所见,对于每个 if/elif,我都需要将捕获组字符串也插入到输出字符串中,但我只能重新评估它以提取捕获的组.如此处所述,可以使用 python 3.8'th Walrus 运算符 (:=),但我仍然使用 Python 3.6.

As you can see, for each if/elif I need the capture group string to be also plugged into the output string, but I see no way but to re-evaluate it in order to extract the captured group. As noted here, python 3.8'th Walrus operator (:=) can be used but I still have Python 3.6.

有没有更优雅的方式只使用一次被评估的组?

Is there a more elegant way to use the evaluated group just once?

推荐答案

通过将 re.search 结果分配给一个变量,然后检查该变量是否不是 可以轻松完成无:

It can easily be done via assigning the re.search result to a variable and then checking if the variable is not None:

m = re_compiled_pat_int.search(my_string)
if m is not None:   # or 'if m:' will do, too
    # Do something

在 Python 3.8 中,有一个选项 使用海象运算符 :=:

In Python 3.8, there is an option to get the match object in the if condition using a walrus operator :=:

if (match := re_compiled_pat_int.search(my_string)) is not None:
    # Do something with match

请参阅更多信息海象运营商介绍:

Python 3.8 有一个新的 walrus 操作符 := 可以给变量作为更大表达式的一部分.匹配时很有用需要两次匹配对象的正则表达式.它也可以与计算值以测试循环终止的 while 循环一起使用然后在循环体中再次需要相同的值.它可以也可用于列表推导式中,其中在 a 中计算的值表达式体中也需要过滤条件.

Python 3.8 has a new walrus operator := that assigns values to variables as part of a larger expression. It is useful when matching regular expressions where match objects are needed twice. It can also be used with while-loops that compute a value to test loop termination and then need that same value again in the body of the loop. It can also be used in list comprehensions where a value computed in a filtering condition is also needed in the expression body.

海象运算符是在 PEP 572(赋值表达式)中提出的作者:Chris Angelico、Tim Peters 和 Guido van Rossum 去年.自从然后它在 Python 社区中被很多人广泛讨论质疑是否需要改进.其他人很兴奋运算符确实使代码更具可读性.

The walrus operator was proposed in PEP 572 (Assignment Expressions) by Chris Angelico, Tim Peters, and Guido van Rossum last year. Since then it has been heavily discussed in the Python community with many questioning whether it is a needed improvement. Others are excited as the operator does make the code more readable.

这篇关于在 Python 中重用正则表达式捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆