问题
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 |'
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?
回答1:
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
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
See more info about the walrus operator introduction:
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.
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.
来源:https://stackoverflow.com/questions/59089096/re-use-of-a-regular-expression-capture-group-in-python