Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

regex - Re-use of a regular expression capture group in Python

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...