Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中重复使用正则表达式捕获组_Python_Regex_Python 3.x_Regex Group - Fatal编程技术网

在Python中重复使用正则表达式捕获组

在Python中重复使用正则表达式捕获组,python,regex,python-3.x,regex-group,Python,Regex,Python 3.x,Regex Group,以下python代码确实有效,但正则表达式“search”计算两次: # 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 =

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

    # 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,我需要将捕获组字符串也插入到输出字符串中,但我认为除了重新评估它以提取捕获的组之外别无选择。正如这里提到的,可以使用Python3.8的Walrus操作符(:=),但我仍然使用Python3.6


是否有一种更优雅的方法只使用一次被评估组?

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

m = re_compiled_pat_int.search(my_string)
if m is not None:   # or 'if m:' will do, too
    # Do something
在Python3.8中,使用walrus操作符获取if条件下的匹配对象
:=

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

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

PEP 572(赋值表达式)中提出了海象算子 由克里斯·安吉利科、蒂姆·彼得斯和吉多·范·罗瑟姆去年创作。自从 然后,它在Python社区中与许多人进行了大量讨论 质疑是否需要改进。其他人则兴奋不已 运算符确实使代码更具可读性


只需使用
m=re\u compiled\u pat\u int.search(我的字符串)
,如果m:,则使用
。。。尽管在Python3.8中,它会做得更优雅,正如上面的评论。如果您需要多次使用正则表达式的结果,那么将结果存储在一个变量中,然后对该变量执行所有操作。在Python 3.6.OK@Wiktor中,您没有其他方法可以做到这一点,这确实可以解决单个“if…”的问题。但我在“if”后面有多个“elif”语句,每个语句都有一个不同的重模式来“搜索”,我不能在它们之间更改“m”。Python中是否有类似Perl的“$1”之类的东西?@MeirG场景似乎有些牵强,因为即使在Perl中,
$1
变量也会在每次成功的正则表达式匹配时重新写入。即使在Perl中,您也不需要这样做。请用真实场景/测试用例更新问题,我相信有更好的方法解决问题。