Is it worth using Python's re.compile?

前端 未结 26 1821
旧时难觅i
旧时难觅i 2020-11-22 12:51

Is there any benefit in using compile for regular expressions in Python?

h = re.compile(\'hello\')
h.match(\'hello world\')

vs



        
26条回答
  •  有刺的猬
    2020-11-22 13:10

    For me, the biggest benefit to re.compile is being able to separate definition of the regex from its use.

    Even a simple expression such as 0|[1-9][0-9]* (integer in base 10 without leading zeros) can be complex enough that you'd rather not have to retype it, check if you made any typos, and later have to recheck if there are typos when you start debugging. Plus, it's nicer to use a variable name such as num or num_b10 than 0|[1-9][0-9]*.

    It's certainly possible to store strings and pass them to re.match; however, that's less readable:

    num = "..."
    # then, much later:
    m = re.match(num, input)
    

    Versus compiling:

    num = re.compile("...")
    # then, much later:
    m = num.match(input)
    

    Though it is fairly close, the last line of the second feels more natural and simpler when used repeatedly.

提交回复
热议问题