How to split long regular expression rules to multiple lines in Python

前端 未结 6 1881
日久生厌
日久生厌 2020-11-30 02:58

Is this actually doable? I have some very long regex pattern rules that are hard to understand because they don\'t fit into the screen at once. Example:

test         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 03:54

    The Python compiler will automatically concatenate adjacent string literals. So one way you can do this is to break up your regular expression into multiple strings, one on each line, and let the Python compiler recombine them. It doesn't matter what whitespace you have between the strings, so you can have line breaks and even leading spaces to align the fragments meaningfully.

提交回复
热议问题